0

I have a python script which does remote command execution, and I want to use multiprocessing in it.

I managed to get the program itself working, but when trying to interrupt it using CTRL+C, I get the following exception, and the program doesn't terminate correctly:

Traceback (most recent call last):
  File "/usr/lib/python2.7/multiprocessing/process.py", line 258, in _bootstrap
    self.run()
  File "/usr/lib/python2.7/multiprocessing/process.py", line 114, in run
    self._target(*self._args, **self._kwargs)
  File "/usr/lib/python2.7/multiprocessing/pool.py", line 102, in worker
    task = get()
  File "/usr/lib/python2.7/multiprocessing/queues.py", line 376, in get
    return recv()
KeyboardInterrupt
Caught KeyboardInterrupt, terminating workers

What am I doing wrong?

Frederik Struck-Schøning
  • 12,981
  • 8
  • 59
  • 68
Mahesh Shitole
  • 762
  • 1
  • 6
  • 15

1 Answers1

0

CTRL+C sends a SIGINT to the python process, which triggers a KeyboardInterrupt exception. Here, your parent process hits this exception, and since you are not catching it, it exits brutally, as well as its children.

If you want to handle CTRL+C, you need to catch this KeyboardInterrupt exception in your parent process. An example is given in this answer for instance. If you share your code we may find something more specific to your case.

Community
  • 1
  • 1
little-dude
  • 1,544
  • 2
  • 17
  • 33