使用 Python 2.6.7 运行时,以下脚本挂起。[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]它在 Python 2.7 中按预期打印。这可能是 Python 2.6 中的错误吗?有什么解决方法吗?
def p1(x):
    return x + 1
class Tasks(object):
    @property
    def mapper(self):
        from multiprocessing import Pool
        pool = Pool(processes=2)
        return pool.imap_unordered
    def run(self):
        xs = range(10)
        return self.mapper(p1, xs)
ts = Tasks()
print(list(ts.run()))
在我的程序中,我可以通过重写Tasks.run来解决挂起:
    def run(self):
        xs = range(10)
        mapper = mapper
        return mapper(p1, xs)
但我无法用上面的脚本重现这个。
另请注意,@property这里的使用是必不可少的。像下面这样分配解决了这个问题mapper:__init__
def __init__(self):
    from multiprocessing import Pool
    pool = Pool(processes=2)
    self.mapper = pool.imap_unordered