12

I'm using the multiprocessing module to do parallel processing in my program. When I'm testing it, I'll often want to kill the program early when I notice a bug, since it takes a while to run to completion. In my Linux environment, I run my program from a terminal, and use Ctrl+C to kill it. With multiprocessing, this causes all the processes to be killed, but I never get the bash prompt back, and have to close the terminal and open a new one (and navigate back to my working directory) which is quite annoying. Is there any way to get around this?

Colin
  • 10,447
  • 11
  • 46
  • 54

3 Answers3

23

Hit Ctrl-Z to suspend the Python process, then do kill %1 to kill it. You can also just hit Ctrl-\ (backslash), but that may cause the process to leave a core file.

llasram
  • 4,417
  • 28
  • 28
3

But wait ... isn't there a way of handling the KeyboardException event to kill the processes in the pool?

I tried:

except KeyboardInterrupt as e: # Ctrl-C
    print("Killing all the children in the pool.")
    pool.close()
    pool.terminate()
    return 1
    # raise e

But it doesn't seem to work.

mathtick
  • 6,487
  • 13
  • 56
  • 101
0

You could use screen to start the process, then kill the screen session when you need to. It won't take out the whole bash terminal. For a good screen tutorial see:

http://www.kuro5hin.org/story/2004/3/9/16838/14935

Jonathan Holloway
  • 62,090
  • 32
  • 125
  • 150