从不可变对象读取时使用multiprocessing.pool映射的错误

时间:2012-04-09 17:57:41

标签: python python-3.x multiprocessing immutability

我正在尝试使用多处理Pool类来映射一些不可变对象的纯函数。然而,当我尝试运行它时,我看到终端中有大量错误(有时会持续几分钟),而且通常,python'必须以一种不寻常的方式终止'。我在Windows(XP)上运行,使用python 3.2.2。

import multiprocessing

def do_stuff(v):
    return v.x + v.y

class Vector:
    __slots__ = ['x', 'y']

    def __setattr__(self, name, value):
        raise AttributeError("Cannot assign values to object {0} of type {1}".format(self, type(self)))

    def __init__(self, x, y = None):
        """Initialize an immutable x, y Vector"""
        object.__setattr__(self, 'x', x)
        object.__setattr__(self, 'y', y)

if __name__ == "__main__":
    todo = [Vector(1, 2), Vector(3, 4), Vector(-1, 12), Vector(16, 32), Vector(16, 32)]
    pool = multiprocessing.Pool(4)
    results = list(pool.map(do_stuff, todo))
    print(results)

预期产出:

[3, 7, 11, 48, 48]

错误的数量非常大,但似乎可以归结为pool.map尝试在Vector上设置属性的内容:

Process PoolWorker-1:
Traceback (most recent call last):
  File "c:\Python32\lib\multiprocessing\process.py", line 267, in _bootstrap
Process PoolWorker-2:
Traceback (most recent call last):
  File "c:\Python32\lib\multiprocessing\process.py", line 267, in _bootstrap
    self.run()
  File "c:\Python32\lib\multiprocessing\process.py", line 116, in run
    self._target(*self._args, **self._kwargs)
  File "c:\Python32\lib\multiprocessing\pool.py", line 102, in worker
    task = get()
  File "c:\Python32\lib\multiprocessing\queues.py", line 378, in get
    return recv()
  File "d:\Documents and Settings\Userdir\Scripts\temp\test.py", line 11, in __s
etattr__
    raise AttributeError("Cannot assign values to object {0} of type {1}".format
(self, type(self)))
AttributeError: Cannot assign values to object <__main__.Vector object at 0x00C2
BBB0> of type <class '__main__.Vector'>

我可以注释掉以__slots__开头的行(奇怪吗?),使用常规map,或者我可以使Vector类可变(注释掉__setattr__),以及这些中的任何一个本身都会使它工作正常。

当我所做的只是从对象中读取时,为什么会尝试在我的对象上设置属性?

为什么从对象中删除__slots__会导致此操作正常工作?

编辑:

我使用__slots__作为节省内存/空间的方法,因为我的程序中通常有大量的Vector。因此,我无法从元组中导出Vector:__slots__ do not work for classes derived from ''variable-length'' built-in types such as long, str and tuple

1 个答案:

答案 0 :(得分:1)

__slots__会导致multiprocessing出现问题,例如参见python multiprocessing pickle protocol

由于你不需要它来实现不变性(你的__setattr__足以满足所有实际目的)我建议你删除它。

如果你真的需要它来节省空间,那么实施__getstate____setstate__可以解决这个问题。