使用python多处理锁定/管理器封装

时间:2013-05-26 13:46:00

标签: 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.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类中封装管理器和锁定语义(即,没有显式地将锁定对象传递给可调用函数)?

0 个答案:

没有答案