【发布时间】:2017-10-26 14:53:20
【问题描述】:
在python3的multiprocess中,不能调用__del__方法。
我已经阅读了有关循环引用的其他问题,但在multiprocess 中找不到情况。
foo中存在循环引用,直接调用foo时会调用__del__,但multiprocess中永远不会调用__del__。
import multiprocessing
import weakref
class Foo():
def __init__(self):
self.b = []
def __del__(self):
print ('del')
def foo():
print ('call foo')
f = Foo()
a = [f]
# a = [weakref.ref(f)]
f.b.append(a)
# call foo in other process
p = multiprocessing.Process(target=foo)
p.start()
p.join()
# call foo
foo()
输出:
调用 foo
调用 foo
删除
为什么在p 中没有调用__del__?
【问题讨论】:
-
你认为
weakref是做什么的?你在代码中提到它,它与这个问题有关,但你没有说为什么你认为它是相关的。
标签: python python-3.x python-multiprocessing circular-reference finalizer