Peter Smit
Peter Smit

Reputation: 28716

How to run a clean up when terminating Python script

I have a python script 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. When I press Ctrl-C, I would like, that every worker immediately cleans up its experiment (which is some custom code, or actually even a subprocess command, not just releasing locks or memory) and stops.

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

Pool.terminate() will not be useful, because the processes will be terminated without cleaning up.

Upvotes: 2

Views: 4394

Answers (2)

Eric O. Lebigot
Eric O. Lebigot

Reputation: 94485

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.)

Upvotes: 1

Eric O. Lebigot
Eric O. Lebigot

Reputation: 94485

How about trying the atexit standard module?

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

Upvotes: 2

Related Questions