Parallel#
- class Parallel(n_cores: int = 3, initializer=None, initargs=(), activate: bool = False)#
Bases:
objectUtility wrapper for paralellizing tasks across multiple CPU cores.
This class simplifies the management of a multiprocessing.Pool, providing a clean interface to activate, execute, and shut down parallel workers.
- Parameters:
n_cores (int, default multiprocessing.cpu_count() - 1) – Number of cores to use.
initializer (callable, optional) – A function to be called by each worker process when it starts.
initargs (tuple, optional) – Arguments to be passed to the initializer.
activate (bool, default False) – Whether to start the pool immediately upon instantiation.
Methods
Create and start the multiprocessing pool.
Gracefully shut down the process pool.
Execute a function in parallel.
- activate()#
Create and start the multiprocessing pool.
This method allocates system resources and spawns the worker processes. It must be called before
performcan be used.
- deactivate()#
Gracefully shut down the process pool.
Closes the pool to new tasks and waits for all current worker processes to exit. Resources are freed and the internal pool is set back to None.
- perform(func: Callable, *args)#
Execute a function in parallel.
If a single iterable is provided in
args,pool.mapis used. If multiple iterables are provided, they are zipped together and executed viapool.starmap.- Parameters:
func (callable) – The function to execute in parallel.
*args (iterable) – The input data to be distributed among workers.
- Returns:
list – The collected results from all worker processes.
- Raises:
AssertionError – If
funcis not callable or no arguments are provided.
Attributes
- max_cores = 4#
Total number of logical CPU cores available on the system.
Calculated by
multiprocessing.cpu_count()