stopping a multiprocessing.managers.BaseManager nicely (looks like a hack)

News123 news123 at free.fr
Sun Mar 7 22:47:51 CET 2010


My fix has certain problems:

News123 wrote:
> 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()
Problems will now occur on he client side.
The server terminates now immediately after the function stopme has been
called.
The client however wants still to perform a few requests, before it
considers calling of stopme done.

So I still don't have a solution :-(

> 
> 
> 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
> 
> 



More information about the Python-list mailing list