@@ -617,14 +617,16 @@ def __init__(self, max_workers=None, mp_context=None,
617
617
execute the given calls. If None or not given then as many
618
618
worker processes will be created as the machine has processors.
619
619
mp_context: A multiprocessing context to launch the workers. This
620
- object should provide SimpleQueue, Queue and Process.
620
+ object should provide SimpleQueue, Queue and Process. Useful
621
+ to allow specific multiprocessing start methods.
621
622
initializer: A callable used to initialize worker processes.
622
623
initargs: A tuple of arguments to pass to the initializer.
623
- max_tasks_per_child: The maximum number of tasks a worker process can
624
- complete before it will exit and be replaced with a fresh
625
- worker process, to enable unused resources to be freed. The
626
- default value is None, which means worker process will live
627
- as long as the executor will live.
624
+ max_tasks_per_child: The maximum number of tasks a worker process
625
+ can complete before it will exit and be replaced with a fresh
626
+ worker process. The default of None means worker process will
627
+ live as long as the executor. Requires a non-'fork' mp_context
628
+ start method. When given, we default to using 'spawn' if no
629
+ mp_context is supplied.
628
630
"""
629
631
_check_system_limits ()
630
632
@@ -644,7 +646,10 @@ def __init__(self, max_workers=None, mp_context=None,
644
646
self ._max_workers = max_workers
645
647
646
648
if mp_context is None :
647
- mp_context = mp .get_context ()
649
+ if max_tasks_per_child is not None :
650
+ mp_context = mp .get_context ("spawn" )
651
+ else :
652
+ mp_context = mp .get_context ()
648
653
self ._mp_context = mp_context
649
654
650
655
if initializer is not None and not callable (initializer ):
@@ -657,6 +662,11 @@ def __init__(self, max_workers=None, mp_context=None,
657
662
raise TypeError ("max_tasks_per_child must be an integer" )
658
663
elif max_tasks_per_child <= 0 :
659
664
raise ValueError ("max_tasks_per_child must be >= 1" )
665
+ if self ._mp_context .get_start_method (allow_none = False ) == "fork" :
666
+ # https://github.com/python/cpython/issues/90622
667
+ raise ValueError ("max_tasks_per_child is incompatible with"
668
+ " the 'fork' multiprocessing start method;"
669
+ " supply a different mp_context." )
660
670
self ._max_tasks_per_child = max_tasks_per_child
661
671
662
672
# Management thread
0 commit comments