views:

127

answers:

2

I have a python scripts that does some jobs. I use multiprocessing.Pool to have a few workers do some commands for me.

My problem is when I try to terminate the script. I would like when I press Ctrl-C, that every worker immediately cleans up it's experiment (which is some custom code, or actually even a subprocess command, not just releasing locks or memory) and stops.

I now that I can catch Ctrl-C with the signal handler. How can make all current running workers of a multiprocessing.Pool to terminate, still doing there cleanup command?

Pool.terminate() does of course not work, because the processes will be terminated without cleaning up.

+1  A: 

How about trying the atexit standard module?

It allows you to register a function that will be executed upon termination.

EOL
No, it says: "Note: the functions registered via this module are not called when the program is killed by a signal". And I need to have it killed by signal
Peter Smit
By 'killed by a signal' I think it means killed by a signal that you don't explicitly catch and handle yourself. If you're using the Python signal handler to catch the Ctrl-C and handle it yourself, you can do what you like in your handler.
Simon Hibbs
A: 

Are you working with Unix? If yes, why not catch SIGTERM in the subprocesses? In fact, the documentation of Process.terminate() reads:

Terminate the process. On Unix this is done using the SIGTERM signal

(I have not tested this.)

EOL