python multiprocessing BaseManager registered class lost connection immediately after Ctrl-C

前端 未结 2 1244
予麋鹿
予麋鹿 2020-12-18 14:34

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 B

相关标签:
2条回答
  • 2020-12-18 14:49

    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
    

    Cheers,

    0 讨论(0)
  • 2020-12-18 15:03

    This is duplicate to question and I wrote there:

    Simplest solution - start manager with

    manager.start(signal.signal, (signal.SIGINT, signal.SIG_IGN))
    

    instead of manager.start(). And check if signal module is in your imports (import signal).

    This catch and ignore SIGINT (Ctrl-C) in manager process.

    0 讨论(0)
提交回复
热议问题