With Clause for Multiprocessing in Python

idude picture idude · Aug 16, 2017 · Viewed 7.1k times · Source

In python 3, you can now open a file safely using the with clause like this:

with open("stuff.txt") as f:
    data = f.read()

Using this method, I don't need to worry about closing the connection

I was wondering if I could do the same for the multiprocessing. For example, my current code looks like:

pool = multiprocessing.Pool(processes=multiprocessing.cpu_count())
pool.starmap(function,list)
pool.close()
pool.join()

Is there any way I could use a with clause to simplify this?

Answer

noxdafox picture noxdafox · Aug 17, 2017
with multiprocessing.Pool( ... ) as pool:
    pool.starmap( ... )

https://docs.python.org/3/library/multiprocessing.html#multiprocessing.pool.Pool

New in version 3.3: Pool objects now support the context management protocol – see Context Manager Types. enter() returns the pool object, and exit() calls terminate().

You can see an example at the bottom of the Pool section.