session.spawn() starts the parent operation; operation.run() starts an additional process in that operation. Starting another independent operation creates a separate runtime.
Complete access setup before using the REST endpoints. The complete interactive-process recipe uses one bounded Python SDK program.
Use an SDK operation handle
The following program keeps a bounded parent alive, sends two input chunks tocat, and checks the child’s result. Configure credentials using the Python quickstart.
first line, second line, and Child exit code: 0. The operation context starts a shared event reader. On exit it signals the parent, waits for shutdown, and attempts cancellation if needed. The parent and child have separate results; stopping the parent ends the shared VM.
Select ContreeAsyncSession for async code, await spawn(), operation.run(), send_stdin(), and child.wait(), and use async with operation. The default lazy session’s spawn() returns a subprocess handle instead of this operation handle.
For large input, use operation.pipe_stdin(chunks, spid=child.spid). It waits for spawn, sends chunks in order, splits them to at most 64 KiB by default, and sends EOF when the source ends. The return value reports acknowledged bytes and EOF delivery, not bytes consumed by the application. Async code accepts an async iterable. The caller owns the input source; stdin writes are never retried automatically.
operation.signal("SIGTERM", spid=child.spid) signals just the child. operation.cancel() requests cancellation of the entire operation. spawn() does not commit session history automatically: for a retained, naturally completed operation, wait and call session.commit_result(operation). Disposable operations leave history unchanged.
Keep the parent alive until the work is done
The primary process controls the operation lifetime. When it finishes, finalization begins and additional subprocesses cannot continue independently. Design the parent with a bounded coordination condition, such as waiting for a completion file until a deadline. A subprocess finishing does not finalize the parent or create its own result image. Before spawning a child, confirm that the parent has reachedASSIGNED or EXECUTING and has published an application readiness signal. Status alone says that the runtime is active; it does not prove that a service or file inside the runtime is ready. A short GET /operations/{operationId}?inflight=1 poll can inspect partial parent output when a dedicated readiness endpoint is unavailable.
Client arrows represent REST requests, not direct connections to guest processes. This is the normal path in the interactive recipe. The file coordinates the two processes. If the primary process ends first, finalization stops any remaining child; the client cannot keep it alive by continuing to poll.
Spawn and address the child
Send anExecSpec to POST /operations/{operationId}/subprocesses. VM-level fields such as image, disposable, timeout, and resources_limits belong to the parent operation and are not accepted in the child request.
A successful response returns a fresh spid of 2 or greater and a Location for its result. Preserve both the parent operation UUID and the child spid. If the create request times out, inspect the event stream for a matching spawn event before retrying because the child may already exist.
See Spawn an additional subprocess.
Send ordered input and EOF
Set the child’s initialstdin.close to false when later writes are required. Send each chunk to POST /operations/{operationId}/subprocesses/{spid}/stdin. Serialize writes to the same child when order matters; concurrent writes have unspecified order. The API does not append a newline.
Use encoding: "ascii" for text and encoding: "base64" for arbitrary bytes. Send close: true on the final write, or send an empty close-only request, to deliver EOF. A 204 confirms the request completed. A 504 leaves delivery uncertain, so a blind retry can duplicate input. Add sequence numbers or another application-level idempotency marker when duplicate input would be harmful.
See Write to subprocess stdin.
Observe completion or signal the process
ReadGET /operations/{operationId}/subprocesses/{spid} until the child result is ready. Honor Retry-After for a 425 response and keep polling bounded. Check the child’s exit code, signal, timeout flag, stdout, stderr, encoding, and truncation separately from the parent result.
To stop a live child, call DELETE /operations/{operationId}/subprocesses/{spid}?signal=SIGTERM or choose another documented signal. The signal targets the subprocess’s process group. Completion is observed later in the child result: a signalled child reports exit_code: -1 and the signal number. Signalling spid=1 ends the primary workload and therefore ends the operation.
See Signal a subprocess.
Handle lifetime races
The interactive-process recipe covers normal two-write input with EOF, a child terminated by
SIGTERM while the parent completes normally, and a negative case where the parent finishes first. Its operation context shuts down the parent when the interaction finishes or raises an error.