I want to terminate python's multiprocessing Pool with ctrl + c (KeyboardInterrupt)

problem

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.

Cause

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.

Solution

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

I want to terminate python's multiprocessing Pool with ctrl + c (KeyboardInterrupt)
I want to remove Python's Unresolved Import Warning with vsCode
I want to do ○○ with Pandas
I want to debug with Python
I want to detect objects with OpenCV
I want to blog with Jupyter Notebook
I want to pip install with PythonAnywhere
I want to analyze logs with Python
I want to play with aws with python
I want to use MATLAB feval with python
I want to analyze songs with Spotify API 2
I want to mock datetime.datetime.now () even with pytest!
I want to display multiple images with matplotlib.
I want to knock 100 data sciences with Colaboratory
I want to make a game with Python
I want to be an OREMO with setParam!
I want to analyze songs with Spotify API 1
I want to use Temporary Directory with Python2
I don't want to use -inf with np.log
#Unresolved I want to compile gobject-introspection with Python3
I want to use ip vrf with SONiC
I want to solve APG4b with Python (Chapter 2)
I want to start over with Django's Migrate
I want to make C ++ code from Python code!
I want to write to a file with Python
I want to convert an image to WebP with lollipop
I want to detect unauthorized login to facebook with Jubatus (1)
I want to transition with a button in flask
I want to handle optimization with python and cplex
I want to climb a mountain with reinforcement learning
I want to inherit to the back with python dataclass
I want to work with a robot in python.
I want to split a character string with hiragana
I want to AWS Lambda with Python on Mac!
I want to manually create a legend with matplotlib
[TensorFlow] I want to process windows with Ragged Tensor
[ML Ops] I want to do multi-project with Python
I want to add my own structure to the structure created by Python's C extension module!
I want to run a quantum computer with Python
I want to bind a local variable with lambda
I want to be able to analyze data with Python (Part 3)
I want to use R functions easily with ipython notebook
I want to specify another version of Python with pyvenv
How to kill a process instantly with Python's Process Pool Executor
I want to be able to analyze data with Python (Part 1)
I want to make a blog editor with django admin
I want to start a jupyter environment with one command
[NetworkX] I want to search for nodes with specific attributes
I want to make a click macro with pyautogui (desire)
I want to change the Japanese flag to the Palau flag with Numpy
I want to be able to analyze data with Python (Part 4)
I want to color black-and-white photos of memories with GAN
I want to be able to analyze data with Python (Part 2)
I want to automatically attend online classes with Python + Selenium!
I want to make a click macro with pyautogui (outlook)
[Python] I want to use the -h option with argparse
I want to use a virtual environment with jupyter notebook!
I want to install a package from requirements.txt with poetry
[Visualization] I want to draw a beautiful graph with Plotly
I want to solve Sudoku (Sudoku)
I want to use a wildcard that I want to shell with Python remove