使用python多处理的锁定/管理器封装
下面的简化示例失败,异常名为使用python多处理的锁定/管理器封装,python,multiprocessing,locks,Python,Multiprocessing,Locks,下面的简化示例失败,异常名为 AttributeError: type object 'SyncManager' has no attribute 'from_address' 示例代码: import multiprocessing as mp class Inserter: def __init__(self): self.cache = {} self.manager = mp.Manager() self.lock = self
AttributeError: type object 'SyncManager' has no attribute 'from_address'
示例代码:
import multiprocessing as mp
class Inserter:
def __init__(self):
self.cache = {}
self.manager = mp.Manager()
self.lock = self.manager.Lock()
def __call__(self, filename):
with self.lock:
print filename
if __name__ == "__main__":
files = [
{'a':1, 'b':2},
{'b':1, 'c':2},
{'d':1, 'e':2},
{'b':1, 'd':2},
]
inserter = Inserter()
pool = mp.Pool(processes=None)
pool.imap_unordered(inserter, files)
pool.close()
pool.join()
我想知道如何在Inserter类中嵌入管理器和锁定语义(即,不将锁定对象显式传递到可调用函数中)?您能写下一个您想要实现的示例吗?我已经得出结论,您无法创建可调用对象,例如,定义了调用的类的实例,同时该实例存储了multiprocessing.Manager和/或multiprocessing.Lock实例,我只是不确定为什么。我试图实现的是一个类,它将锁/管理器隐藏在其对象中,并与多处理.Pool兼容(以某种方式),这样我就不必在main中实例化管理器/锁。@artonson,我遇到了完全相同的问题。你能找到解决办法吗?