If you write python's multiprocessing Pool normally and ctrl + c during execution, the process will not end and will remain stuck.
pool.py
import time
from multiprocessing import Pool
def worker(arg):
print("process {}".format(arg))
time.sleep(2)
if __name__ == '__main__':
p = Pool(2)
p.map(worker, range(6))
p.close()
(If you Ctrl + C while running the above source)
(Abbreviation)
KeyboardInterrupt
(It will stop in this state)
Even if I specify the process ID and kill it, python revives like a zombie. I want to finish it cleanly with Ctrl + C somehow.
Someone had exactly the same worries on StackOverflow. https://stackoverflow.com/questions/1408356/keyboard-interrupts-with-pythons-multiprocessing-pool
It seems that the input of KeyboardInterrupt is not transmitted to the process called in Pool, and the wait function keeps waiting forever.
It's OK if you set a timeout.
p.map(worker, range(6))
To
p.map_async(worker, range(6)).get(9999999)
Replace with.
Recommended Posts