如果我使用getattr实现对象组合并将对象传递到一个新进程,我会从getattr得到一个 RecursionError 。这是一个例子:
from multiprocessing import Pool
class Bar:
def __init__(self, bar):
self.bar = bar
class Foo:
def __init__(self, bar):
self._bar = bar
def __getattr__(self, attr):
try:
return getattr(self._bar, attr)
except RecursionError:
print('RecursionError while trying to access {}'.format(attr))
raise AttributeError
def f(foo):
print(foo.bar)
if __name__ == '__main__':
foo = Foo(Bar('baz'))
f(foo)
p = Pool(1)
p.map(f, [foo])
输出是这样的:
baz
RecursionError while trying to access _bar
baz
为什么 Foo 找不到 _bar 属性而不得不求助于getattr?