python - How can I cleanly exit a Pyro Daemon by client request? -


i'm trying use pyro control slave machine. rsync necessary python files, start pyro server, perform actions remote control, , want tell pyro server shut down.

i'm having trouble getting pryo daemon shut down cleanly. either hangs in daemon.close() call, or if comment out line exits without shutting down socket correctly, resulting in socket.error: [errno 98] address in use if restart server soon.

it don't think so_reuseaddr right fix, unclean socket shutdown still results in socket hanging around in time_wait state, potentially causing clients experience problems. think better solution convince pyro daemon close socket properly.

is improper call daemon.shutdown() within daemon itself?

if start server , press ctrl-c without clients connected don't have problems (no address in use errors). makes clean shutdown seem possible, of time (assuming otherwise sane client , server).

example: server.py

import pyro4  class testapi:     def __init__(self, daemon):         self.daemon = daemon     def hello(self, msg):         print 'client said {}'.format(msg)         return 'hola'     def shutdown(self):         print 'shutting down...'         self.daemon.shutdown()  if __name__ == '__main__':     daemon = pyro4.daemon(port=9999)     tapi = testapi(daemon)     uri = daemon.register(tapi, objectid='testapi')     daemon.requestloop()     print 'exited requestloop'     daemon.close() # hangs     print 'daemon closed' 

example: client.py

import pyro4  if __name__ == '__main__':         uri = 'pyro:testapi@localhost:9999'         remote = pyro4.proxy(uri)         response = remote.hello('hello')         print 'server said {}'.format(response)         try:             remote.shutdown()         except pyro4.errors.connectionclosederror:             pass         print 'client exiting' 

i think can done without using timeout or loopcondition, having shutdown() call daemon's shutdown. according http://pythonhosted.org/pyro4/servercode.html#cleaning-up:

another possibility calling pyro4.core.daemon.shutdown() on running bdaemon object. break out of request loop , allows code neatly clean after itself, , work on threaded server type without other requirements.

the following works on python3.4.2 on windows. @pyro4.oneway decorator shutdownis not needed here, in situations.

server.py

import pyro4 # using python3.4.2  @pyro4.expose class testapi:     def __init__(self, daemon):         self.daemon = daemon     def hello(self, msg):         print('client said {}'.format(msg))         return 'hola'     @pyro4.oneway   # in case call returns later daemon.shutdown     def shutdown(self):         print('shutting down...')         self.daemon.shutdown()  if __name__ == '__main__':     daemon = pyro4.daemon(port=9999)     tapi = testapi(daemon)     uri = daemon.register(tapi, objectid='testapi')     daemon.requestloop()     print('exited requestloop')     daemon.close()     print('daemon closed') 

client.py

import pyro4 # using python3.4.2  if __name__ == '__main__':     uri = 'pyro:testapi@localhost:9999'     remote = pyro4.proxy(uri)     response = remote.hello('hello')     print('server said {}'.format(response))     remote.shutdown()     remote._pyrorelease()     print('client exiting') 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

c# - How do I get the Nth largest element from a list with duplicates, using LINQ? -

jsp - "Sending a redirect is forbidden after the response has been committed" in sendRedirect -