Skip to main content
A function fits batch jobs, background work, and GPU tasks that your code or a person calls. HTTP callers suit an endpoint, and recurring runs a schedule.

Define a function

Save this as reports.py:

Call it

From the CLI, JSON arguments become Python values:
From Python:
Both run in a cloud container, stream its logs to your terminal, and return three sales totaling 5500 cents. async_remote() is the async form, and a plain summarize_sales(...) call runs locally. Where a call goes covers previews and deployments.

Process a batch

.map() submits one call per input and yields results in input order. Each tuple holds one call’s arguments:
A failed call yields None. When every item must succeed before you use the batch, spawn_map() returns each outcome to check:
Without an autoscaler the calls run one at a time in one container. An autoscaler lets the function start more containers and run them in parallel:
A .map() over 1,000 texts now runs on up to 20 containers at once. max_pending_tasks goes up because .map() submits every input at once. See Scaling and the Parquet example.

Submit work in the background

.spawn() returns a FunctionCall right away, and .get() waits for the value. uv run lazycloud task result <task-id> collects it from anywhere else, and a pending FunctionCall passed to another function arrives as its result. Tasks and logs covers looking calls up by ID.

Arguments and results

Calls from the Python SDK carry any serializable Python value, including NumPy arrays, dataclasses, custom class instances, tuples, and bytes:
The caller needs the same packages and class definitions. Results are unpickled, so they should come from code you trust. HTTP calls, lazycloud run --json, and exported clients need JSON, which Pydantic models and dataclasses convert to. Arguments and results cap at 16 MiB each, so larger output goes in a volume or an artifact.

Function options

Retries and timeouts

timeout_seconds caps one attempt, and retries, 3 by default, adds attempts after a failure. One call can therefore run four times, so writes should be safe to repeat, for example keyed on the input, or retries=0. A RetryPolicy adds backoff:
A preempted call retries too, even when its code never failed.

Lifecycle hooks

on_start runs once before a container accepts calls, which suits loading a model or opening a client:
Per-call hooks are on_running, on_success, on_error, on_retry, on_failure, and on_finish. lazycloud.current_task_id() returns the current call’s ID inside the function or a hook.

Stop and clean up

lazycloud task cancel cancels a pending call and task stop a running one. A deployed function stays callable until stopped:
stop keeps the definition for deployment start. --prune removes a function deleted from the code.

Shared settings

Images, secrets, volumes, resources, and scaling.