简体   繁体   中英

Multiprocessing pool with async workers

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.

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM