Skip to main content

gcejob

Run a script or a container on a short-lived Google Cloud VM. Logs stream back, outputs go to Cloud Storage, and the VM and its disk are deleted when the job ends. You pay only while it runs.

Good for training and evaluation jobs that need machine shapes (like small GPUs) that managed training services don't offer.

How it works

your laptop                           Google Cloud
-----------                           ------------
gcejob.ScriptRun(...).start()
  1. upload code           ───────►   Cloud Storage (staging)
  2. submit job            ───────►   Cloud Batch
                                        3. creates a VM (+ GPU driver) with your service account
                                        4. mounts your buckets at /gcs/<bucket> and /job/...
                                        5. runs your command (on the VM, or in your container)
  6. wait, print status    ◄───────      status events
  7. print logs            ◄───────   Cloud Logging
                                        8. outputs are already in Cloud Storage
                                        9. VM and boot disk are deleted

Batch owns the VM lifecycle, so there is nothing to clean up by hand when a job finishes or fails.

Setup

You need:

  1. Python 3.10+ and the gcloud CLI.
  2. A GCP project with billing and these APIs enabled:
    gcloud services enable batch.googleapis.com compute.googleapis.com \
        logging.googleapis.com storage.googleapis.com
    
  3. A service account for the VMs with these roles:
    • roles/batch.agentReporter (without it, jobs hang while the VM bills)
    • roles/logging.logWriter
    • read/write access to your bucket
    • roles/artifactregistry.reader (only for private images)
  4. A bucket in the same region as the VM (your code is uploaded there).
  5. For you: roles/batch.jobsEditor, roles/logging.viewer, and roles/iam.serviceAccountUser on the service account.
  6. GPU quota and free capacity, if you use GPUs.

Then:

pip install -e .
gcloud auth application-default login

Run a script

import gcejob

gcejob.configure(
    project="my-project",
    region="us-west1",
    staging_bucket="gs://my-bucket",
    service_account="sa-gcejob-runner@my-project.iam.gserviceaccount.com",
)

run = gcejob.ScriptRun("hello", script="train.py", requirements=["numpy"])
run.start(machine="e2-standard-2", args=["--epochs", "1"])

start() waits, prints the status changes, then prints your script's output.

Run a container

run = gcejob.ContainerRun(
    "eval",
    image="us-west1-docker.pkg.dev/my-project/repo/train:latest",
    command=["python", "-m", "train"],
)
run.start(machine="g2-standard-4", args=["--split", "val"], buckets=["my-bucket"])

Your data

Buckets are mounted as files. Your code reads and writes them like normal folders:

Path in the job What it is
/gcs/<bucket>/... A whole bucket (list it in buckets=[...])
/job/input/<name> A path you pass in inputs={"name": "gs://..."}
/job/output Saved to <output_dir>/model when the job ends
/job/checkpoints Saved to <output_dir>/checkpoints

Command line

gcejob run job.yaml          # run a job from a YAML file
gcejob status <job-id>       # state, and why if it is stuck
gcejob logs <job-id> -f      # follow logs
gcejob cancel <job-id>
gcejob list
gcejob cleanup --stuck --older-than 30m --yes    # cancel jobs that never started

Commands that take a job id need --project and --region (or GCEJOB_PROJECT, GCEJOB_REGION).

Cost safety

  • Billing starts when the VM is created, so boot time counts.
  • Jobs are on-demand by default. Spot is opt-in: provisioning="spot".
  • max_run (default 24h) caps the job. startup_timeout (default 15m) cancels a job that never starts.
  • If you submit with wait=False, run gcejob cleanup --stuck on a schedule.
  • Keep buckets and VMs in the same region, or reads are billed per GB.
  • Set a billing budget alert on the project.

Good to know

Jobs

  • Every job gets a fresh VM, so nothing carries over between jobs. Save anything you need to /job/output, /job/checkpoints or a bucket.
  • Your script runs as root, in /job/code, with bash. The job id is <name>-<timestamp>.
  • Logs are printed when the job finishes. To watch them live, use gcejob logs <job-id> -f.
  • One VM per job. There is no multi-node training.
  • On Spot, a preempted job is re-run from the start (up to max_retries, default 3). Your script must resume from its own checkpoints.

Machines

  • The boot disk must be at least 30 GB (disk_gb, default 100).
  • Allow 3 to 10 minutes before your code starts: VM boot, plus GPU driver install on GPU machines. Set startup_timeout to match (for example "20m").
  • G2, G4, A2 and A3 machines include their GPU. Other families need an N1 machine plus accelerator=....
  • Some newer machine families only accept Hyperdisk boot disks. Set disk_type if the job is rejected.
  • Quota is not capacity. Having GPU quota doesn't mean Google has free GPUs in the zone.

Data

  • Only buckets you list (buckets, inputs, output_dir) are mounted. Anything else is missing from the VM.
  • Buckets are mounted with Cloud Storage FUSE: fast for large sequential files (such as 512 MB shards), slow for many small files and random access.
  • Your uploaded code stays in <staging_bucket>/gcejob/staging/. Add a lifecycle rule to expire it.

Access

  • Your login needs a quota project. gcejob uses the job's project automatically when yours has none.
  • The VMs run as the service account you pass, not as you. Give it access to every bucket and image the job uses.

Troubleshooting

You see Do this
Job stays SCHEDULED The service account is missing batch.agentReporter or logging.logWriter.
capacity in gcejob status Google has no free machines of that type in the zone. Retry later or try another region.
PERMISSION_DENIED on submit Grant yourself batch.jobsEditor and serviceAccountUser.
Disk cannot be smaller than the chosen image Use disk_gb of 30 or more.
Quota exceeded ... project_number:<number> Run gcloud auth application-default set-quota-project <your-project>.
Missing /gcs/<bucket> or /job/input/... The bucket isn't in buckets, inputs or output_dir, or the service account can't read it.

Status

Version 0.0.1 (early release), Cloud Batch only. Checked on real GCP: CPU jobs, code upload, the startup timeout. Still being validated or not yet tested on real hardware: bucket mounts, container jobs, GPU jobs, and Spot retry.

Development

pip install -e ".[dev]"
pytest

Metadata

Release files for gcejob 0.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for gcejob 0.0.1
File Size Uploaded Provenance Actions
gcejob-0.0.1.tar.gz 37.6 kB Without provenance Details for gcejob-0.0.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for gcejob 0.0.1
File Interpreter ABI Platform Actions
gcejob-0.0.1-py3-none-any.whl Python 3 none any Details for gcejob-0.0.1-py3-none-any.whl

Total release size: 66.1 kB

Release files / gcejob-0.0.1.tar.gz

Download URL gcejob-0.0.1.tar.gz
Size 37.6 kB
Tags Source
SHA-256 checksum
How to use checksums
b4b039173a5e9d4c95b3382f1a1ec0b9b0a4a9f3e116ddf747207bd1dabbb4f9
BLAKE2b-256 checksum
How to use checksums
79f25dabd1336faa4ab6d691247e1fc962d091049d085e9a0ac650b9ee3ae58a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.8

Release files / gcejob-0.0.1-py3-none-any.whl

Download URL gcejob-0.0.1-py3-none-any.whl
Size 28.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4ecdec84ed631b5b7ba3c0fe35bec71b8da23667c4ce17dd511ec68c226cb907
BLAKE2b-256 checksum
How to use checksums
926bf1d62195b80be561ba464b0d8767957ced627d0ba348db2968f24202042d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.8

Security for gcejob 0.0.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page