Skip to content
Advertisement

Graceful cleanup for multiprocess pool python

I am running a multiprocessing pool, mapped over a number of inputs. My worker processes have an initialization step that spins up a connection to selenium and a database. When the pool finishes its job, what is the graceful way to close these connections rather than just relying on python’s memory management and del definitions?

EDIT:

JavaScript

Because some_args is large, I only want to call shutdown when the worker processes have no other jobs to do. I don’t want to close / reopen connections to my database until everything is done.

As of right now, I would expect the memory manager to call __del__ if the worker process shutsdown, but I don’t know if it does occur. I’ve gotten strange scenarios where it hasn’t been called. I’m hoping to better understand how to manage shutdown.

Advertisement

Answer

I think you have a good chance of closing your drivers if you first wait for your pool processes to terminate and then force a garbage collection:

JavaScript

Solution With User-created Pool

JavaScript

Prints:

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement