From 0a7b2edbe034cda34047feb1a06321a19e081624 Mon Sep 17 00:00:00 2001 From: Yumeng Xu Date: Tue, 20 Aug 2024 23:57:11 +0200 Subject: [PATCH] Add check before setting multiprocessing context to prevent the RuntimeError (#4620) * Add check before setting multiprocessing context to prevent the runtime error: context has already been set When `pycbc` is used with some multiprocessing package such as `dask`, importing `pycbc` will cause setting the multiprocessing context repeatedly. The `RuntimeError: context has already been set` will happen. This fix will check the context before setting it. * Update __init__.py Change raising error to warning * wrap the lines --------- Co-authored-by: Yumeng Xu --- pycbc/__init__.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/pycbc/__init__.py b/pycbc/__init__.py index d6db0026e0a..99f4cc6ef37 100644 --- a/pycbc/__init__.py +++ b/pycbc/__init__.py @@ -212,8 +212,13 @@ def makedir(path): # preserve common state information which we have relied on when using # multiprocessing based pools. import multiprocessing - if hasattr(multiprocessing, 'set_start_method'): - multiprocessing.set_start_method('fork') + if multiprocessing.get_start_method(allow_none=True) is None: + if hasattr(multiprocessing, 'set_start_method'): + multiprocessing.set_start_method('fork') + elif multiprocessing.get_start_method() != 'fork': + warnings.warn("PyCBC requires the use of the 'fork' start method" + " for multiprocessing, it is currently set to {}" + .format(multiprocessing.get_start_method())) else: HAVE_OMP = True