Gracefull termination of multiprocessing.Pool.map function using KeyboardInterrupt

84 Views Asked by At

I'm trying to terminate gracefully Pool.map() function using KeyboardInterrupt.

import multiprocessing
import time


def func(num):
    try:
        while True:
            print(f'Process num {num}')
            time.sleep(30)
    except KeyboardInterrupt:
        print(f'Process {num} was interrupted')
    finally:
        print('Cleaning up the thread')


def main():
    pool = multiprocessing.Pool()

    try:
        pool.map(func, range(9))
    except KeyboardInterrupt:
        print('Main process was interrupted)
        pool. Close()

    finally:
        print('Cleaning up main')


if __name__ == '__main__':
    main()

While it works fine with range(8) (cuz I have 8 logical cores and only 8 processes running simultaneously). After pressing Ctrl+C, I receive the following output:

python experiment.py

Process num 0
Process num 1
Process num 2
Process num 3
Process num 4
Process num 5
Process num 6
Process num 7
Process 7 was interrupted
Process 6 was interrupted
Cleaning up the thread
Process 4 was interrupted
Cleaning up the thread
Process 5 was interrupted
Process 3 was interrupted
Process 2 was interrupted
Cleaning up the thread
Process 0 was interrupted
Process 1 was interrupted
Cleaning up the thread
Cleaning up the thread
Cleaning up the thread
Cleaning up the thread
Cleaning up the thread
Main process was interrupted
Cleaning up main

However, when I use range(9), after pressing Ctrl+C it interrupts only 8 processes that are already running, while map function keeps on running generating a new process, and after pressing Ctrl+C once again I get the following output:

Process num 0
Process num 1
Process num 2
Process num 3
Process num 4
Process num 5
Process num 6
Process num 7
Process 6 was interrupted
Process 7 was interrupted
Process 5 was interrupted
Cleaning up the thread
Cleaning up the thread
Process 2 was interrupted
Cleaning up the thread
Process 3 was interrupted
Process 1 was interrupted
Cleaning up the thread
Process 0 was interrupted
Process num 8
Cleaning up the thread
Cleaning up the thread
Process 4 was interrupted
Cleaning up the thread
Cleaning up the thread
Process 8 was interrupted
Process SpawnPoolWorker-7:
Process SpawnPoolWorker-6:
Process SpawnPoolWorker-4:
Process SpawnPoolWorker-5:
Cleaning up the thread
Process SpawnPoolWorker-3:
Process SpawnPoolWorker-2:
Process SpawnPoolWorker-1:
Main process was interrupted
Cleaning up main
Traceback (most recent call last):
Traceback (most recent call last):
Traceback (most recent call last):
  File "C:\Users\Zamotory\AppData\Local\Programs\Python\Python310\lib\multiprocessing\process.py", line 314, in _bootstrap
    self. Run()

Is there any way to stop Pool.map() after the single press of Ctrl+C?

0

There are 0 best solutions below