Skip to main content

gcejob

PyPI License

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.

Quickstart

pip install gcejob
gcloud auth application-default login
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")

gcejob.ScriptRun("hello", script="train.py").start(machine="e2-standard-2")

This needs a GCP project, a service account and a bucket. See Setup first.

Example output (your script's output streams in while the job is RUNNING):

submitted projects/my-project/locations/us-west1/jobs/hello-20261117-104509
hello-20261117-104509: QUEUED
hello-20261117-104509: STARTING
hello-20261117-104509: RUNNING
<your script's output, line by line as it runs>
hello-20261117-104509: SUCCEEDED

Status lines and log lines are fetched separately, so the last few log lines can appear just before or after SUCCEEDED.

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.

Once that is done, install and log in as shown in the Quickstart. From a source checkout, use pip install -e . instead of pip install gcejob.

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

Describe a job in YAML and run it without writing Python:

# job.yaml
name: cpu-smoke
project: my-project
region: us-west1
service_account: sa-gcejob-runner@my-project.iam.gserviceaccount.com
machine: e2-standard-4
max_run: 5m
command: |
  echo CPU_SMOKE_OK

More in examples/.

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                  # job id, name, state
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 stream live while the job runs (a few seconds behind, since they go through Cloud Logging). To follow a job from another terminal, 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

Experimental (0.0.1), Cloud Batch only. Verified on real GCP: CPU jobs, code upload and the startup timeout. Not yet verified on real hardware: live log streaming, bucket mounts, container jobs, GPU jobs and Spot retry. Expect rough edges and report them as issues.

Development

pip install -e ".[dev]"
pytest

License

Apache-2.0. See LICENSE.

Metadata

Release files for gcejob 0.0.4

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.4
File Size Uploaded Provenance Actions
gcejob-0.0.4.tar.gz 42.1 kB Without provenance Details for gcejob-0.0.4.tar.gz

Built distribution (wheel)

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

Total release size: 71.8 kB

Release files / gcejob-0.0.4.tar.gz

Download URL gcejob-0.0.4.tar.gz
Size 42.1 kB
Tags Source
SHA-256 checksum
How to use checksums
4591ca61618bd09582b3a4f8a34231ead996f91310fc2eb1955a11bea26698d2
BLAKE2b-256 checksum
How to use checksums
bc7573350c9e92d30ae134733885e823ae456aab6d2e6c194a0bcb99a788932a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.16

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

Download URL gcejob-0.0.4-py3-none-any.whl
Size 29.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6c70b57c733a0ed936989ac113e16fb1ab188009ef4149ee25cb2df47534934b
BLAKE2b-256 checksum
How to use checksums
9c04a26b6fa9714ee1acb07dd7e01296344d0ee9ec76975e04b05fdf7f324a41
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.16

Security for gcejob 0.0.4

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