Matt Seymour
Matt Seymour

Reputation: 9395

Exiting a multiprocessed python application safely

I noticed that os._exit(<num>) ::

Exit the process with status n, without calling cleanup handlers, flushing stdio buffers, etc.

and that sys.exit() ::

“only” raises an exception, it will only exit the process when called from the main thread

I need a solution to close a multi-processed application that will ensure all processes are closed (none left orphaned) and that it exits in the best state possible.

Extras:

I am creating the processes using the python multiprocessing library, by creating classes which inherit from multiprocessing.Process

Upvotes: 4

Views: 1214

Answers (1)

Mike Sandford
Mike Sandford

Reputation: 1365

I ended up creating a Pipe for every Process. Then when the main Process shuts down it can send a message to all the children Processes that they should shut down too.

In order to make that work right you've got to put a periodic check into the children Processes' "do loop" to see if there are messages in the pipe, and if so, check them to see if it's a "quit now" message.

Upvotes: 1

Related Questions