Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 47b162a

Browse files
committed
gh-128041 - Add a terminate_workers method to ProcessPoolExecutor
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
1 parent be8ae08 commit 47b162a

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

Lib/concurrent/futures/process.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
import weakref
5858
from functools import partial
5959
import itertools
60+
import signal
6061
import sys
6162
from traceback import format_exception
6263

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

857858
shutdown.__doc__ = _base.Executor.shutdown.__doc__
859+
860+
def terminate_workers(self, signal=signal.SIGINT):
861+
"""Attempts to terminate the executor's workers using the given signal.
862+
Iterates through all of the current processes and sends the given signal if
863+
the process is still alive.
864+
865+
After terminating workers, the pool will be in a broken state and no longer usable.
866+
867+
Args:
868+
signal: The signal to send to each worker process. Defaults to
869+
signal.SIGINT.
870+
"""
871+
if self._processes:
872+
for pid, proc in self._processes.items():
873+
try:
874+
is_alive = proc.is_alive()
875+
except ValueError:
876+
# The process is already exited/closed out.
877+
is_alive = False
878+
879+
if is_alive:
880+
try:
881+
os.kill(pid, signal)
882+
except ProcessLookupError:
883+
# The process just ended before our signal
884+
pass

0 commit comments

Comments
 (0)