I am working with concurrent futures multiprocessing and I want to implement graceful exit on SIGINT while also killing all non started processes. This is achieved by the executor shutdown as shown in the code snippet. The problem is with the additional process that is spun up after the shutdown from the queue because of
EXTRA_QUEUED_CALLS = 1
and the SIGINT is not captured by this final process as it is still in the queue when the signal was captured.
import signal
import time
from concurrent.futures import ProcessPoolExecutor
def thread_worker(tenant):
print("Wokring on tenant: ", tenant)
def signal_handler(sig: signal, frame: any) -> None:
print("Interrupt received in sub process")
signal.signal(
signal.SIGINT,
lambda signum, frame: signal_handler(signum, frame),
)
time.sleep(2)
def multi_process():
with ProcessPoolExecutor(2) as process_executor:
def signal_handler(process_executor, sig: signal, frame: any) -> None:
print("Interrupt received")
process_executor.shutdown(wait=True, cancel_futures=True)
print("Killed all processes")
signal.signal(
signal.SIGINT,
lambda signum, frame: signal_handler(process_executor, signum, frame),
)
process_futures = [
process_executor.submit(thread_worker, tenant) for tenant in range(5)
]
if __name__ == "__main__":
multi_process()
The output
Wokring on tenant: 0
Wokring on tenant: 1
^CInterrupt received in sub process
Interrupt received in sub process
Interrupt received
Wokring on tenant: 2
Killed all processes