cotengra.parallel¶
Interface for parallelism.
This module centralizes cotengra’s lightweight parallel execution logic. The
main user entry point is parse_parallel_arg, which turns parallel
arguments such as False, True, "auto", "threads",
"loky", "concurrent.futures", "dask", and "ray" into a
pool-like executor or None. Local process pools are cached and reused in
the process that created them, while distributed backends try to discover an
existing scheduler or runtime before creating one explicitly.
The "auto" mode remembers the last persistent non-thread backend and uses
it for future automatic parallel work. To avoid recursive process pools,
worker subprocesses are marked when tasks are submitted through submit;
inside such workers parallel="auto" resolves to serial execution. A PID
guard provides the same protection for forked children that inherit module
state. Threads remain explicit-only and are not remembered as the automatic
backend.
The helper functions submit, scatter, can_scatter, and
should_nest provide a small common interface over local, dask, and ray
executors while preserving backend-specific behavior where needed.
Attributes¶
Classes¶
Basic |
|
Basic |
Functions¶
|
|
|
Get a parallel pool. |
|
|
|
Return the backend type of |
|
Extract how many workers our pool has (mostly for working out how many |
|
|
|
Create a parallel pool of type |
|
Logic required for nested parallelism in dask.distributed. |
|
Logic required for nested parallelism in dask.distributed. |
|
Run |
|
Interface for submitting |
|
Interface for maybe turning |
|
Whether |
|
Given argument |
|
|
|
|
|
Maybe get an existing or create a new dask.distrbuted client. |
|
|
Allows passing futures by reference - takes e.g. args and kwargs and |
|
|
Cached retrieval of remote function. |
|
Alternative for 'non-function' callables - e.g. partial |
|
Maybe get an existing or create a new RayExecutor, thus initializing, |
Module Contents¶
- cotengra.parallel._AUTO_BACKEND = None¶
- cotengra.parallel._AUTO_BACKEND_PID = None¶
- cotengra.parallel._IS_WORKER = False¶
- cotengra.parallel.have_loky¶
- cotengra.parallel.have_joblib¶
- cotengra.parallel._DEFAULT_BACKEND = 'loky'¶
- cotengra.parallel.get_pool(n_workers=None, maybe_create=False, backend=None)[source]¶
Get a parallel pool.
- cotengra.parallel.get_n_workers(pool=None)[source]¶
Extract how many workers our pool has (mostly for working out how many tasks to pre-dispatch).
- cotengra.parallel.set_parallel_backend(backend)[source]¶
Create a parallel pool of type
backendand register persistent backends as the default for'auto'parallel.The
'threads'backend remains explicit-only and is never remembered as the default for'auto'.
- cotengra.parallel.maybe_leave_pool(pool)[source]¶
Logic required for nested parallelism in dask.distributed.
- cotengra.parallel.maybe_rejoin_pool(is_worker, pool)[source]¶
Logic required for nested parallelism in dask.distributed.
- cotengra.parallel._worker_call(fn, args, kwargs)[source]¶
Run
fninside a worker, marking the process as a worker first.The marker is intentionally sticky for the lifetime of the worker process: any later
parallel="auto"work in the same subprocess should remain serial rather than creating nested process pools.
- cotengra.parallel.submit(pool, fn, *args, **kwargs)[source]¶
Interface for submitting
fn(*args, **kwargs)topool.
- cotengra.parallel.scatter(pool, data)[source]¶
Interface for maybe turning
datainto a remote object or reference.
- class cotengra.parallel.CachedProcessPoolExecutor[source]¶
- _pool = None¶
- _n_workers = -1¶
- _pid = None¶
- cotengra.parallel.ProcessPoolHandler¶
- class cotengra.parallel.CachedThreadPoolExecutor[source]¶
- _pool = None¶
- _n_workers = -1¶
- _pid = None¶
- cotengra.parallel.ThreadPoolHandler¶
- cotengra.parallel._get_pool_dask(n_workers=None, maybe_create=False)[source]¶
Maybe get an existing or create a new dask.distrbuted client.
- class cotengra.parallel.RayFuture(obj)[source]¶
Basic
concurrent.futureslike future wrapping a rayObjectRef.- __slots__ = ('_obj', '_cancelled')¶
- _obj¶
- _cancelled = False¶
- cotengra.parallel._unpack_dispatch¶
- cotengra.parallel._unpack_futures(x)[source]¶
Allows passing futures by reference - takes e.g. args and kwargs and replaces all
RayFutureobjects with their underylingObjectRefwithin all nested tuples, lists and dicts.[Subclassing
ObjectRefmight avoid needing this.]
- cotengra.parallel.get_deploy(**remote_opts)[source]¶
Alternative for ‘non-function’ callables - e.g. partial functions - pass the callable object too.
- class cotengra.parallel.RayExecutor(*args, default_remote_opts=None, **kwargs)[source]¶
Basic
concurrent.futureslike interface usingray.- default_remote_opts¶
- _maybe_inject_remote_opts(remote_opts=None)[source]¶
Return the default remote options, possibly overriding some with those supplied by a
submit call.
- submit(fn, *args, pure=False, remote_opts=None, **kwargs)[source]¶
Remotely run
fn(*args, **kwargs), returning aRayFuture.
- cotengra.parallel._RAY_EXECUTOR = None¶
- cotengra.parallel._get_pool_ray(n_workers=None, maybe_create=False)[source]¶
Maybe get an existing or create a new RayExecutor, thus initializing, ray.
- Parameters:
- Return type:
None or RayExecutor