@@ -617,14 +617,16 @@ def __init__(self, max_workers=None, mp_context=None,
617617 execute the given calls. If None or not given then as many
618618 worker processes will be created as the machine has processors.
619619 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.
621622 initializer: A callable used to initialize worker processes.
622623 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.
628630 """
629631 _check_system_limits ()
630632
@@ -644,7 +646,10 @@ def __init__(self, max_workers=None, mp_context=None,
644646 self ._max_workers = max_workers
645647
646648 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 ()
648653 self ._mp_context = mp_context
649654
650655 if initializer is not None and not callable (initializer ):
@@ -657,6 +662,11 @@ def __init__(self, max_workers=None, mp_context=None,
657662 raise TypeError ("max_tasks_per_child must be an integer" )
658663 elif max_tasks_per_child <= 0 :
659664 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." )
660670 self ._max_tasks_per_child = max_tasks_per_child
661671
662672 # Management thread
0 commit comments