vayu.parallel¶
parallel
¶
SafeCounter
¶
Bases: ABC
Concurrency-safe keyed counter.
Two implementations are chosen by make(executor_cls):
ThreadSafeCounter (lock-guarded dict) for threads and
ProcessSafeCounter (multiprocessing.Manager dict) for processes.
make
staticmethod
¶
make(executor_cls: type) -> SafeCounter
Return a counter matched to the given executor class.
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in vayu/parallel.py
SafeDict
¶
Bases: ABC
Concurrency-safe keyed dict.
Two implementations are chosen by make(executor_cls):
ThreadSafeDict (lock-guarded dict) for threads and ProcessSafeDict
(multiprocessing.Manager dict) for processes.
make
staticmethod
¶
make(executor_cls: type) -> SafeDict
Return a dict matched to the given executor class.
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in vayu/parallel.py
ParallelRunner
¶
ParallelRunner(
n_workers: int = None,
executor_cls: type = None,
initializer: Callable = None,
init_args: tuple = None,
return_when: str = ALL_COMPLETED,
print_progress_every: float = 5,
report_exceptions: bool = True,
)
Simplified wrapper over ThreadPoolExecutor / ProcessPoolExecutor.
Adds keyed task submission, a background progress line, and captured
exceptions (failed tasks store the exception as their result rather than
raising through run()).
A single ParallelRunner instance runs once: you call add() any
number of times, then exactly one run(). Calling add() after
run(), or run() twice, raises RuntimeError.
Construct a runner.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_workers
|
int
|
Worker count. Defaults to the executor's default. |
None
|
executor_cls
|
type
|
Either |
None
|
initializer
|
Callable
|
Optional worker initializer (e.g. for loading process-local state). |
None
|
init_args
|
tuple
|
Positional args forwarded to |
None
|
return_when
|
str
|
Forwarded to |
ALL_COMPLETED
|
print_progress_every
|
float
|
Progress-line refresh interval, seconds. |
5
|
report_exceptions
|
bool
|
If True, log failures via the |
True
|
Source code in vayu/parallel.py
add
¶
Queue a task to be run by run(). Keys must be unique; later adds overwrite.
Source code in vayu/parallel.py
run
¶
Execute all queued tasks and return a dict of {key: result_or_exception}.
Exceptions from individual tasks are captured (not raised). If the
calling thread gets a KeyboardInterrupt, the runner cancels pending
futures and re-raises.
Source code in vayu/parallel.py
shutdown
¶
Stop the underlying executor. Passes through to Executor.shutdown.