Skip to content

parallel

The process pools of sbmlsim.

Every pool of the package comes from here:

  • process_context is the start method of every pool, never fork of a process which runs threads;
  • pool(n) is a pool kept per process and number of workers, so that the start of the workers (the imports, roadrunner) is paid once per process and not once per run; start_pool(n) is a new pool which the caller stops, e.g. the pool of a fit, whose workers keep the initialized problem; start_pool_soon(n) starts one in a thread while the caller goes on, e.g. the pool of a run of experiments while they are initialized;
  • resolve_workers turns n_workers into the number of processes;
  • worker_cache keeps objects in a worker, e.g. the models of a scan or the initialized problem of a fit, and builds one only when its key is new;
  • worker_cores is the share of the CPUs of a worker for threads of its own: the CPUs which the workers of its pool which run a task (working) leave in python go to the ones which run threads (threads), also to a process which runs a task beside its pool (beside).

Ctrl-C belongs to the process of the user: a terminal sends SIGINT to every process of the job, the workers ignore it and the parent stops a pool whose run it interrupts, see start_pool and stop. On POSIX a worker starts with SIGINT blocked, so Ctrl-C does not reach it while it still imports, see _SigintBlocked.

A pool starts worker processes which import the main module again (the start methods forkserver and spawn), so a script which starts a pool must do it behind the guard if __name__ == "__main__":. Without it the workers die while they start and the parent reports the guard, see GUARD_MESSAGE. A worker itself never starts a pool, a task which runs in a worker runs serially.

PendingPool

PendingPool(n_workers, start, then=None)

A pool which starts in a thread of its own while its caller goes on.

The start of a pool waits for its workers: the forkserver imports the modules of the workers (preload, about 2 s for the experiments of a model of pkdb_models) and the first worker answers. A caller which has other work before it needs the pool, e.g. a run of experiments which initializes them, starts it with start_pool_soon and takes it with take when it needs it, so the workers start meanwhile.

The thread only starts processes: under fork it would fork a process which runs another thread, so start_pool_soon refuses that start method. The caller stops a pool it did not take with stop.

Start the pool in a thread.

Parameters:

Name Type Description Default
n_workers int

the number of worker processes.

required
start Callable[[], ProcessPoolExecutor]

starts the pool, e.g. start_pool with its arguments.

required
then Callable[[ProcessPoolExecutor], object] | None

called with the pool in the thread once it started, e.g. to submit the first tasks.

None

take

take()

Wait for the pool to start and take it: the caller stops it from now on.

Raises:

Type Description
RuntimeError

if the workers do not start (see start_pool), or the pool was taken or stopped already.

stop

stop()

Wait for the start of the pool and stop it, see stop.

shared_payloads

shared_payloads(sources)

Share immutable snapshots with local workers without repeated IPC copies.

The caller keeps this context until submitted work has finished or its workers have stopped. The temporary directory is private to this process. Equal snapshots share one file, independently of integrator settings.

Parameters:

Name Type Description Default
sources Sequence[str]

the snapshots, e.g. the SBML of the models of a run.

required

Yields:

Type Description
Generator[list[str]]

The path of the file of every snapshot, in the order of sources; the

Generator[list[str]]

files are removed when the context exits, also after an error.

process_context

process_context()

Get the multiprocessing context a pool of sbmlsim is created from.

A start method set with multiprocessing.set_start_method is used, else the default of the platform: forkserver on linux, spawn on macOS and windows. No default forks, the process of a simulation runs the threads of roadrunner and of the linear algebra, and a fork of a process with threads may deadlock in the child.

Every pool of sbmlsim comes from this module, see pool and start_pool, and the start method of the process is not fixed by the call (get_context() without a method would), because a start method which is set is read as one the user chose.

Returns:

Type Description
BaseContext

The context.

in_worker

in_worker()

Check whether this process is a child process of multiprocessing.

worker_cores

worker_cores()

Get the cores this process may use for threads of its own.

A worker of a pool shares the CPUs of the quota with the workers which run a task (working): one which runs no threads of its own takes one CPU, its python, and the ones which run threads (threads, this process counted) share the others, at least one each, so the threads of a busy pool are about the CPUs and the long experiment of a run takes the CPUs which the workers in python or out of tasks leave, at its next batch (sbmlsim#664). With no worker counted busy it has the 1/n share of a pool of n processes. A process which runs a task beside a pool of its own (beside) counts among the workers and has a share as well; any other process has every CPU of the quota. The threads of the batches of sbmlsim-core in a worker take this share (sbmlsim.simulator.scan_core.core_threads).

Returns:

Type Description
int

The number of cores, at least one.

shares_cpus

shares_cpus()

Check whether this process shares the CPUs with the workers of a pool.

A worker of a pool does, and a process while it runs a task beside a pool of its own (beside): its threads take its share of the CPUs, worker_cores.

working

working()

Count this worker among the workers of its pool which run a task.

A task of a pool whose threads follow the CPUs the other workers leave (worker_cores) runs in it, e.g. an experiment of the runner; outside of a worker of start_pool it counts nothing.

threads

threads()

Count this process among the processes of its pool which run threads.

The batches of sbmlsim-core run in it, whose threads share the CPUs which the busy processes of the pool in python leave (worker_cores); outside of a worker of start_pool and of beside, or within it already, it counts nothing.

beside

beside(executor)

Count this process among the workers of its pool which run a task.

A process which runs a task while the workers of a pool it started run theirs, e.g. the experiment which a run of experiments keeps in its own process, shares the CPUs with them: its threads take its share (worker_cores), and the share of every worker counts it. Outside of a pool of start_pool of this process, or while it runs a task beside one already, it counts nothing.

Parameters:

Name Type Description Default
executor ProcessPoolExecutor

the pool, started by start_pool.

required

check_workers

check_workers(n_workers)

Check a number of workers.

Parameters:

Name Type Description Default
n_workers int | None

None or the number of processes.

required

Raises:

Type Description
ValueError

if n_workers is less than 1.

resolve_workers

resolve_workers(n_workers, n_tasks)

Get the number of processes of a run.

Parameters:

Name Type Description Default
n_workers int | None

1 runs serially in the calling process, a number is taken as given, None is the number of CPUs for POOL_THRESHOLD tasks or more and serial below, and serial in a worker process.

required
n_tasks int

the number of tasks of the run, e.g. the points of a scan.

required

Returns:

Type Description
int

The number of processes, 1 for a serial run.

Raises:

Type Description
ValueError

if n_workers is less than 1.

preload_experiments

preload_experiments()

Preload the experiment classes of the process, the forkserver after its imports.

Calls preload_in_forkserver of every subclass of SimulationExperiment which the process imported, in the order of their names, so that the workers the forkserver forks inherit what they load, e.g. the model which pkpdsim equilibrates a condition on (1 to 2 s of every worker of a run of the HCTZ model of pkdb_models). A process which imported no experiment preloads nothing and imports none. An error of a class is logged, the others are preloaded and a worker loads what failed itself; the forkserver must not end, every pool of the process needs it.

Returns:

Type Description
list[str]

The names of the classes whose preload failed.

start_pool

start_pool(
    n_workers, preload=(), initializer=None, initargs=()
)

Start a pool whose workers answer, see pool for one which is kept.

The workers ignore SIGINT, Ctrl-C interrupts the parent only, see the module.

Parameters:

Name Type Description Default
n_workers int

the number of worker processes.

required
preload Sequence[str]

modules the forkserver imports once for all workers; it takes effect only when the forkserver starts for the first time in this process.

()
initializer Callable[..., object] | None

a module level function every worker calls when it starts, e.g. to keep a queue to the parent, which a worker can only receive when it starts (a queue cannot be sent with a task).

None
initargs tuple[Any, ...]

the arguments of the initializer.

()

Returns:

Type Description
ProcessPoolExecutor

The pool, the caller stops it with stop.

Raises:

Type Description
RuntimeError

in a worker process (no nested pools), or if the workers die while they start or do not start within WORKER_STARTUP_TIMEOUT; both are what a script without the guard does, see GUARD_MESSAGE.

start_pool_soon

start_pool_soon(
    n_workers,
    preload=(),
    initializer=None,
    initargs=(),
    then=None,
)

Start a pool in the background, see PendingPool and start_pool.

Parameters:

Name Type Description Default
n_workers int

the number of worker processes.

required
preload Sequence[str]

modules the forkserver imports once for all workers.

()
initializer Callable[..., object] | None

a module level function every worker calls when it starts.

None
initargs tuple[Any, ...]

the arguments of the initializer.

()
then Callable[[ProcessPoolExecutor], object] | None

called with the pool once it started, in the thread which started it, e.g. to submit the first tasks.

None

Returns:

Type Description
PendingPool | None

The pool which starts, None in a worker or under the start method

PendingPool | None

fork, which does not fork a process which runs threads: the caller

PendingPool | None

starts its pool with start_pool when it needs it.

pool

pool(n_workers)

Get the pool of the process with a number of workers.

The pool is kept, so the workers start once per process; a pool which broke, e.g. because a worker died, is replaced. Every pool is stopped when the process ends, see shutdown.

Parameters:

Name Type Description Default
n_workers int

the number of worker processes.

required

Returns:

Type Description
ProcessPoolExecutor

The pool.

Raises:

Type Description
RuntimeError

see start_pool.

stop

stop(executor)

Stop a pool: the pending tasks are cancelled and the workers end.

The workers are terminated, so a pool whose workers still run, e.g. after Ctrl-C or a timeout, stops at once. A kept pool is dropped, the next pool starts a new one.

The thread of the pool which manages its workers is the only one which waits for them to end, and stop waits for that thread: when it returns, the workers have ended and the pool knows it. A second thread which waited for a worker would race with it under the start method spawn on POSIX: the one which loses finds no process to wait for, and the worker looks alive for a moment after it ended.

shutdown

shutdown()

Stop every kept pool of the process.

worker_cache

worker_cache(key, factory)

Get an object of the worker process, built once per key.

The newest WORKER_CACHE_SIZE objects are kept.

Parameters:

Name Type Description Default
key Hashable

identifies the object, e.g. a model with the settings of its integrator.

required
factory Callable[[], T]

builds the object when the key is new.

required

Returns:

Type Description
T

The object of the key.