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.
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:
submitted projects/my-project/locations/us-west1/jobs/hello-20261117-104509
hello-20261117-104509: QUEUED
hello-20261117-104509: STARTING
hello-20261117-104509: RUNNING
hello-20261117-104509: SUCCEEDED
<your script's output>
final state: 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:
- Python 3.10+ and the gcloud CLI.
- A GCP project with billing and these APIs enabled:
gcloud services enable batch.googleapis.com compute.googleapis.com \ logging.googleapis.com storage.googleapis.com - 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)
- A bucket in the same region as the VM (your code is uploaded there).
- For you:
roles/batch.jobsEditor,roles/logging.viewer, androles/iam.serviceAccountUseron the service account. - 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
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, rungcejob cleanup --stuckon 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/checkpointsor 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_timeoutto 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_typeif 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.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| gcejob-0.0.3.tar.gz | 41.7 kB | Without provenance | Details for gcejob-0.0.3.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| gcejob-0.0.3-py3-none-any.whl | Python 3 | none | any | Details for gcejob-0.0.3-py3-none-any.whl |
Total release size: 71.1 kB
Release files / gcejob-0.0.3.tar.gz
| Download URL | gcejob-0.0.3.tar.gz |
|---|---|
| Size | 41.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
f37676054b4caaafa56865f245e65bb05f3c978ce3bf992ddfde52e443d10fc4
|
|
BLAKE2b-256 checksum How to use checksums |
f8b7bd5f91bcbff1d17c4d2ae226ed3a978313e7e0fac957ab4db6a32d1cd7fe
|
| 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.3-py3-none-any.whl
| Download URL | gcejob-0.0.3-py3-none-any.whl |
|---|---|
| Size | 29.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
822ce4da6b0a715a88c089a047e2d2d5500ece3ccd2772da03093ef54843cd5e
|
|
BLAKE2b-256 checksum How to use checksums |
c0e73ab7adb86aa3fe108a74e40349e977cd69ad12231afef8707509884051e3
|
| 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.3
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.