我遇到了一些我怀疑是我的 python 程序无法正确处理的问题,我的程序无法在按下 Ctrl-C 后立即调用 BaseManager 的注册类的方法,甚至其他进程实现为继承的类从 multiprocessing.Process 受到影响。我有一些我想从 Ctrl-C 后无法正确执行的进程调用的方法。
例如下面的代码不能在 Ctrl-C 之后调用 TestClass 的 mt 实例。
from multiprocessing.managers import BaseManager, NamespaceProxy
import time
class TestClass(object):
def __init__(self, a):
self.a = a
def b(self):
print self.a
class MyManager(BaseManager): pass
class TestProxy(NamespaceProxy):
# We need to expose the same __dunder__ methods as NamespaceProxy,
# in addition to the b method.
_exposed_ = ('__getattribute__', '__setattr__', '__delattr__', 'b')
def b(self):
callmethod = object.__getattribute__(self, '_callmethod')
return callmethod('b')
MyManager.register('TestClass', TestClass, TestProxy)
if __name__ == '__main__':
manager = MyManager()
manager.start()
t = TestClass(1)
print t.a
mt = manager.TestClass(2)
print mt.a
mt.a = 5
mt.b()
try:
while 1:
pass
except (KeyboardInterrupt, SystemExit):
time.sleep(0.1)
mt.a = 7
mt.b()
print "bye"
pass
Here is the console output
1
2
5
^CTraceback (most recent call last):
File "testManager.py", line 38, in <module>
mt.a = 7
File "/usr/lib/python2.7/multiprocessing/managers.py", line 1028, in __setattr__
return callmethod('__setattr__', (key, value))
File "/usr/lib/python2.7/multiprocessing/managers.py", line 758, in _callmethod
conn.send((self._id, methodname, args, kwds))
IOError: [Errno 32] Broken pipe
你有什么建议吗?我的代码中是否有任何解决方法或问题?
提前致谢。