2

以下简化示例失败,并出现名为的异常

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

4

0 回答 0