Skip to content Skip to sidebar Skip to footer

Optimizing Multiprocessing.pool With Expensive Initialization

Here is a complete simple working example import multiprocessing as mp import time import random class Foo: def __init__(self): # some expensive set up function in th

Solution 1:

The intended way to deal with things like this is via the optional initializer and initargs arguments to the Pool() constructor. They exist precisely to give you a way to do stuff exactly once when a worker process is created. So, e.g., add:

def init():
    global foo
    foo = Foo()

and change the Pool creation to:

pool = mp.Pool(4, initializer=init)

If you needed to pass arguments to your per-process initialization function, then you'd also add an appropriate initargs=... argument.

Note: of course you should also remove the

foo = Foo()

line from f(), so that your function uses the global foo created by init().

Solution 2:

most obvious, lazy load

_foo = Nonedeff(y):
    global _foo
    ifnot _foo:
       _foo = Foo()
    return _foo.run(y)

Post a Comment for "Optimizing Multiprocessing.pool With Expensive Initialization"