multiprocessing_manager#

class gammapy.utils.parallel.multiprocessing_manager(backend=None, pool_kwargs=None, method=None, method_kwargs=None)[source]#

Bases: object

Context manager to update the default configuration for multiprocessing.

Only the default configuration will be modified, if class arguments like n_jobs and parallel_backend are set they will overwrite the default configuration.

Parameters:
backend{‘multiprocessing’, ‘ray’}

Backend to use.

pool_kwargsdict

Keyword arguments passed to the pool. The number of processes is limited to the number of physical CPUs.

method{‘starmap’, ‘apply_async’}

Pool method to use.

method_kwargsdict

Keyword arguments passed to the method

Examples

import gammapy.utils.parallel as parallel
from gammapy.estimators import FluxPointsEstimator

fpe = FluxPointsEstimator(energy_edges=[1, 3, 10] * u.TeV)

with parallel.multiprocessing_manager(
        backend="multiprocessing",
        pool_kwargs=dict(processes=2),
    ):
    fpe.run(datasets)