問題描述
我遇到了一些我懷疑是我的 python 程序無法正確處理的問題,我的程序無法在按下 Ctrl-C 后立即調(diào)用 BaseManager 注冊類的方法,即使其他進(jìn)程實(shí)現(xiàn)為從 multiprocessing.Process 繼承的類會受到影響.我有一些我想從 Ctrl-C 后無法正確執(zhí)行的進(jìn)程調(diào)用的方法.
I am experiencing some issues that I suspect is a limitation of my python program to handle correctly, my program is not been able to call methods of a registered class of BaseManager immediately after I hit Ctrl-C, even other process implemented as classes that inherit from multiprocessing.Process are affected. I have some methods that I would like to call from process that don't execute correctly after Ctrl-C.
例如下面的代碼在Ctrl-C之后不能調(diào)用TestClass的mt實(shí)例.
For example the following code is not able to call the mt instance of TestClass after Ctrl-C.
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
你有什么建議嗎?我的代碼中是否有任何解決方法或問題?
Do you have any suggestion? Is there any workaround or something wrong in my code?
提前致謝.
推薦答案
如果有人遇到這個問題,我根據(jù)這個答案解決了 https://stackoverflow.com/a/21106459/1667319.這是工作代碼
If someone happen to had this issue, I solved based on this answer https://stackoverflow.com/a/21106459/1667319 . Here is the working code
from multiprocessing.managers import SyncManager, NamespaceProxy
import time
import signal
#handle SIGINT from SyncManager object
def mgr_sig_handler(signal, frame):
print 'not closing the mgr'
#initilizer for SyncManager
def mgr_init():
signal.signal(signal.SIGINT, mgr_sig_handler)
#signal.signal(signal.SIGINT, signal.SIG_IGN) # <- OR do this to just ignore the signal
print 'initialized mananger'
class TestClass(object):
def __init__(self, a):
self.a = a
def b(self):
print self.a
class MyManager(SyncManager): 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(mgr_init)
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
干杯,
這篇關(guān)于python multiprocessing BaseManager注冊類在Ctrl-C后立即失去連接的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!