Prev: killing own process in windows
Next: stopping a multiprocessing.managers.BaseManager nicely (lookslike a hack)
From: News123 on 7 Mar 2010 16:26 Hi, I have following program from multiprocessing.managers import BaseManager def myfunc(): return 3 class MyManager(BaseManager): pass MyManager.register('myfunc',callable = myfunc) m = MyManager(address=('127.0.0.1', 50000),authkey='abracadabra') server = m.get_server() server.serve_forever() I'd like to replace server.serve_forever() with something, which is abortable. After digging in the sources I came up with following (in my opinion) inelegant, but working solution. I just copied the Server.serve_forever() function from multiprocessing/managers.py and changed two lines. Does anybody have a better idea than this: # ------------------------------------------------ import multiprocessing.managers from multiprocessing.managers import BaseManager def serve_till_stop(self): ''' Run the server forever ''' #current_process()._manager_server = self # this lin removed multiprocessing.managers.current_process()._manager_server = self # this line added try: try: #while 1: # this line removed while self.running: # this line added try: c = self.listener.accept() except (OSError, IOError): continue t = threading.Thread(target=self.handle_request, args=(c,)) t.daemon = True t.start() except (KeyboardInterrupt, SystemExit): pass finally: self.stop = 999 self.listener.close() def myfunc(): return 3 def stopme(): server.running = False class MyManager(BaseManager): pass MyManager.register('myfunc',callable = myfunc) m = MyManager(address=('127.0.0.1', 50000),authkey='abracadabra') server = m.get_server() server.running = True serve_till_stop(server) thanks in advance and bye N |