Skip to content

Commit

Permalink
pythongh-128041 - Add a terminate_workers method to ProcessPoolExecutor
Browse files Browse the repository at this point in the history
Provides a way to forcefully stop all the workers in the pool

Typically this would be used as a last effort to stop all workers if unable to shutdown / join in the expected way
  • Loading branch information
csm10495 committed Dec 17, 2024
1 parent be8ae08 commit 47b162a
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions Lib/concurrent/futures/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import weakref
from functools import partial
import itertools
import signal
import sys
from traceback import format_exception

Expand Down Expand Up @@ -855,3 +856,29 @@ def shutdown(self, wait=True, *, cancel_futures=False):
self._executor_manager_thread_wakeup = None

shutdown.__doc__ = _base.Executor.shutdown.__doc__

def terminate_workers(self, signal=signal.SIGINT):
"""Attempts to terminate the executor's workers using the given signal.
Iterates through all of the current processes and sends the given signal if
the process is still alive.
After terminating workers, the pool will be in a broken state and no longer usable.
Args:
signal: The signal to send to each worker process. Defaults to
signal.SIGINT.
"""
if self._processes:
for pid, proc in self._processes.items():
try:
is_alive = proc.is_alive()
except ValueError:
# The process is already exited/closed out.
is_alive = False

if is_alive:
try:
os.kill(pid, signal)
except ProcessLookupError:
# The process just ended before our signal
pass

0 comments on commit 47b162a

Please sign in to comment.