Skip to content

Commit

Permalink
Add check before setting multiprocessing context to prevent the Runti…
Browse files Browse the repository at this point in the history
…meError (#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 <yumeng.xu@physik.uzh.ch>
  • Loading branch information
xuyumeng and Yumeng Xu authored Aug 20, 2024
1 parent ff8e499 commit 0a7b2ed
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions pycbc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 0a7b2ed

Please sign in to comment.