Skip to main content

spawn

Starting the compute's children as processes.

How a Compute runs its pod and its patient-data API: as separate child processes it can start, watch and stop independently of each other. One implementation for every host. The only thing a host may vary is what runs inside a child before its real work begins -- the bootstrap on a ChildSpec -- which is how the desktop app registers its progress hooks and token queue in the child, and Docker registers nothing.

What the compute itself needs to tell a child (the pod's identity) goes the same way, as a prepare step run in the child ahead of the host's bootstrap, so starting one child never changes anything the host or its other child sees.

A child dies with its host. Each is handed the read end of a pipe whose write end only the host holds (the lifeline), and shuts itself down when that end closes, which the operating system does for a host that exits however it exits -- a crash or a kill included.

Attributes: LIFELINE_EXIT_GRACE_SECONDS: How long a child whose host has gone gives its own shutdown before exiting outright.

Module​

Functions​

child_context​

def child_context() ‑> multiprocessing.context.SpawnContext:

The multiprocessing context every compute child is started under.

Always spawn, whatever the platform default: a child is a fresh interpreter, not a copy of its host. Anything a host hands to a child that carries a lock (a queue, an event) must be built under this same context, because a queue built under fork cannot be passed to a spawn-started process. Hosts build such objects through this function rather than naming the start method themselves.

spawn_child​

def spawn_child(    spec: ChildSpec,    *,    daemon: bool = True,    prepare: Callable[..., None] | None = None,    prepare_args: tuple[Any, ...] = (),) ‑> Spawned:

Start a child process for spec.

Started under child_context, so the child is a fresh interpreter rather than a copy of the host. multiprocessing.freeze_support() remains the host's responsibility, in its main module.

The child is handed the read end of its lifeline; the write end is returned, and the child shuts down when it closes. A caller that drops it stops the child.

Arguments

  • spec: What to run.
  • daemon: Whether the child is torn down automatically when the host exits. A daemon process may not start processes of its own, so this suits the API child but not the pod, which does.
  • prepare: Optional compute-owned setup run in the child before the host's bootstrap. Reaches the child by pickling, like the spec, so it must be a module-level function.
  • prepare_args: Positional arguments for prepare.

Returns The started process and the host's end of its lifeline.

spec_for_spawn​

def spec_for_spawn(source: ChildSpecSource) ‑> ChildSpec:

The spec to start the next process from.

Arguments

  • source: A spec, or a function building a fresh one.

Classes​

ChildSpec​

class ChildSpec(    name: str,    target: Callable[..., None],    args: tuple[Any, ...] = (),    bootstrap: Callable[..., None] | None = None,    bootstrap_args: tuple[Any, ...] = (),):

What to run in a child process, and how to prepare it.

Everything here reaches the child by pickling, so target and bootstrap must be module-level functions and the arguments plain data.

Arguments

  • name: The process name, for logs and status.
  • target: The child's main.
  • args: Positional arguments for target.
  • bootstrap: Optional host-specific setup run in the child before target. The one place hosts differ.
  • bootstrap_args: Positional arguments for bootstrap.

Variables​

  • name : str
  • target : Callable[..., None]

Spawned​

class Spawned(process: BaseProcess, lifeline: Connection):

A started child, and the host's end of its lifeline.

Arguments

  • process: The started process.
  • lifeline: The write end of the pipe the child watches. The child shuts down when it closes, so the host keeps it for as long as it wants the child alive, and closes it to stop the child.

Variables​

  • process : multiprocessing.process.BaseProcess