Home > Net >  Multiprocessing pool with async workers
Multiprocessing pool with async workers

Time:09-28

Consider the following code:

class Sandbox:
    def __init__(self):
        self.pool = Pool(4)

    def worker(self, x):
        print(x) # Should be printing "testing123"

    def run(self):
        res = self.pool.apply_async(self.worker, ("testing123",))
        print(res.get()) # NotImplementedError
        self.pool.close()
        self.pool.join()


sandbox = Sandbox()
sandbox.run()

When I run this, I get

NotImplementedError: pool objects cannot be passed between processes or pickled

res.get() reveals the exception - if I remove it nothing happens.

CodePudding user response:

The async_apply gets the state and also sets the state, so you need to modify those methods, like:

from multiprocessing import Pool
class Sandbox:
    def __init__(self):
        self.pool = Pool(4)

    def worker(self, x):
        print(x) # Should be printing "testing123"

    def run(self):
        res = self.pool.apply_async(self.worker, ("testing123",))
        print(res.get()) # NotImplementedError
        self.pool.close()
        self.pool.join()

    def __getstate__(self):
        self_dict = self.__dict__.copy()
        del self_dict['pool']
        return self_dict

    def __setstate__(self, state):
        self.__dict__.update(state)


sandbox = Sandbox()
sandbox.run()
  • Related