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

N

News123

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
 
N

News123

My fix has certain problems:
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 :-(
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,705
Latest member
Stefkari24

Latest Threads

Top