簡體   English   中英

具有異步工作者的多處理池

[英]Multiprocessing pool with async workers

考慮以下代碼:

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

當我運行這個時,我得到

NotImplementedError:池對象不能在進程之間傳遞或腌制

res.get() 揭示了異常——如果我刪除它,什么也不會發生。

async_apply獲取 state 並設置 state,因此您需要修改這些方法,例如:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM