Skip to content

Commit

Permalink
Prefer forkserver over fork for multiprocessing
Browse files Browse the repository at this point in the history
Use of 'fork' is deprecated for multithreaded
processes and also appears to cause issues with
CUDA startup, so use 'forkserver' instead if available.
  • Loading branch information
benmwebb committed Mar 25, 2024
1 parent fb673cf commit 2992b43
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions modules/multifit/pyext/src/fit_fft.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

multiproc_exception = None
try:
from multiprocessing import Pool
import multiprocessing
# Detect whether we are running Windows Python via Wine. Wine does not
# currently support some named pipe functions which the multiprocessing
# module needs: http://bugs.winehq.org/show_bug.cgi?id=17273
Expand All @@ -23,6 +23,15 @@
multiproc_exception = str(detail)


def _get_context():
# Use 'forkserver' rather than 'fork' start method if we can; 'fork' does
# not work well with multithreaded processes or CUDA
if 'forkserver' in multiprocessing.get_all_start_methods():
return multiprocessing.get_context('forkserver')
else:
return multiprocessing.get_context()


class Fitter(object):

def __init__(
Expand Down Expand Up @@ -161,8 +170,10 @@ def run(asmb_fn, options):
if multiproc_exception is None and options.cpus > 1:
# No point in spawning more processes than components
nproc = min(options.cpus, asmb_input.get_number_of_component_headers())
p = Pool(processes=nproc)
ctx = _get_context()
p = ctx.Pool(processes=nproc)
_ = list(p.imap_unordered(do_work, work_units))
p.close()


def main():
Expand Down

0 comments on commit 2992b43

Please sign in to comment.