Runtime backends

Where a run executes: this machine, a container, or a Slurm job array.

abmfact.runtime

Runtime backends: local, container and Slurm. The container and Slurm backends load on first use.

abmfact.runtime.base

Backends: where a prepared run executes (this machine, a container, or a cluster).

class abmfact.runtime.base.Backend[source]

Bases: ABC

Base class for the places a prepared run can execute.

name

The short backend name recorded on each RunHandle.

Type:

str

abstractmethod availability() → Availability[source]

Report whether this backend can run here now, with a reason and, where known, a version.

abstractmethod submit(prepared: PreparedRun) → RunHandle[source]

Start a run. Local and container backends finish it before returning.

wait(handle: RunHandle) → RunHandle[source]

Block until a submitted run finishes. Backends that finish in submit return it unchanged.

abmfact.runtime.local

The local backend: in-process calls, native commands as subprocesses, and the v0.1 process pool.

class abmfact.runtime.local.LocalBackend(workers: int = 1, timeout: float | None = None)[source]

Bases: Backend

Run on this machine.

An in-process call runs directly and its exceptions propagate unchanged, as v0.1 adapters always behaved. A native argv runs in the prepared work directory with stdout and stderr in run.log.

availability() → Availability[source]

Report whether this backend can run here now, with a reason and, where known, a version.

submit(prepared: PreparedRun) → RunHandle[source]

Start a run. Local and container backends finish it before returning.

map(task: Callable[[J], R], jobs: Sequence[J], progress: Callable[[int, int], None] | None = None) → list[R][source]

Apply task to every job, in a process pool when workers > 1. Results keep job order.

The task is pickled once and installed in each worker. A task that cannot be pickled (a lambda, say) runs serially in this process instead.

abmfact.runtime.local.run_command(prepared: PreparedRun, argv: list[str], backend: str, timeout: float | None) → RunHandle[source]

Run argv in the work directory, logging to run.log; never raises on a failed run.

abmfact.runtime.container

The container backend: run a prepared command inside a Docker, Podman or Apptainer image.

The run’s work directory is mounted at /work and used as the working directory, so an engine prepares the same files and relative command for local and container runs. The image digest is recorded in the run handle, which pins the engine build in the run record.

abmfact.runtime.container.REPOSITORY = 'https://github.com/a7med7emedan/ABMfact.git'

The repository whose containers/ folder holds the engine images’ Dockerfiles.

abmfact.runtime.container.build_command(image: str, folder: str, platform: str | None = None, version: str | None = None) → str[source]

The docker build command for an engine image, with its Dockerfile fetched from GitHub.

A release builds from its own tag, so the image matches the installed code. A development version has no tag and builds from main. Docker fetches containers/<folder> itself, so no checkout is needed.

abmfact.runtime.container.detect_runtime(which: ~collections.abc.Callable[[str], str | None] = <function which>, runner: ~collections.abc.Callable[[...], ~typing.Any] = <function run>) → str | None[source]

The first usable container runtime: Docker or Podman with a running daemon, or Apptainer.

class abmfact.runtime.container.ContainerBackend(image: str, runtime: str = 'auto', platform: str | None = None, timeout: float | None = None, runner: ~collections.abc.Callable[[...], ~typing.Any] = <function run>, which: ~collections.abc.Callable[[str], str | None] = <function which>)[source]

Bases: Backend

Run prepared commands in image.

runtime is "auto" or one of docker, podman, apptainer, singularity. For Apptainer the image is a .sif file. Extra read-only mounts come from prepared.context["mounts"] (host to container).

property is_oci: bool

Whether the runtime is Docker or Podman, which use OCI images rather than .sif files.

availability() → Availability[source]

Report whether this backend can run here now, with a reason and, where known, a version.

image_digest() → str | None[source]

The registry digest when the image was pulled, else its local id; a file hash for .sif.

command(prepared: PreparedRun) → list[str][source]

The full command line that runs prepared.argv in the image.

submit(prepared: PreparedRun) → RunHandle[source]

Start a run. Local and container backends finish it before returning.

abmfact.runtime.container.parse_inspect(text: str) → str | None[source]

Parse {{json .RepoDigests}}|{{.Id}}: the first repo digest’s sha256:..., else the image id.

abmfact.runtime.slurm

The Slurm backend: write one job array for many prepared runs, and submit it only when asked.

Each array task reads its line from manifest.tsv (task index, work directory, command), changes to the work directory and runs the command with output in run.log. Collect the runs with the engine once the array has finished.

class abmfact.runtime.slurm.SlurmBackend(partition: str | None = None, time: str = '01:00:00', mem: str = '4G', cpus_per_task: int = 1, account: str | None = None, max_parallel: int | None = None, job_name: str = 'abmfact', directives: ~collections.abc.Sequence[str] = (), container: ~abmfact.runtime.container.ContainerBackend | None = None, runner: ~collections.abc.Callable[[...], ~typing.Any] = <function run>)[source]

Bases: Backend

Run prepared native commands as a Slurm job array, optionally inside a container.

The settings become #SBATCH lines: partition, time, mem, cpus_per_task and account. max_parallel caps how many array tasks run at once, and directives adds raw #SBATCH options.

availability() → Availability[source]

Report whether this backend can run here now, with a reason and, where known, a version.

write_array_script(prepared: Sequence[PreparedRun], path: str | Path) → Path[source]

Write path (the sbatch script) and manifest.tsv beside it. Nothing is submitted.

submit_array(prepared: Sequence[PreparedRun], path: str | Path, submit: bool = False) → list[RunHandle][source]

Write the array script; with submit=True also run sbatch and record the job id.

submit(prepared: PreparedRun) → RunHandle[source]

Start a run. Local and container backends finish it before returning.

wait(handle: RunHandle) → RunHandle[source]

Mark a run finished once its log exists; Slurm itself is not polled.