I'm trying to use Pyro to control a slave machine. I rsync the necessary python files, start a Pyro server, perform some actions by remote control, and then I want to tell the Pyro server to shut down.
I'm having trouble getting the Pryo Daemon to shut down cleanly. It either hangs in the Daemon.close()
call, or if I comment out that line it exits without shutting down its socket correctly, resulting in socket.error: [Errno 98] Address already in use
if I restart the server too soon.
It don't think that SO_REUSEADDR is the right fix, as unclean socket shutdown still results in a socket hanging around in the TIME_WAIT state, potentially causing some clients to experience problems. I think the better solution is to convince the Pyro Daemon to close its socket properly.
Is it improper to call Daemon.shutdown() from within the daemon itself?
If I start a server and then press CTRL-C without any clients connected I don't have any problems (no Address already in use
errors). That makes a clean shutdown seem possible, most of the time (assuming an otherwise sane client and 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() # this 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'
Address already in use
for the Pyro server, but I get it all the time for theName Server
. Hitting CTRL+C on the NameServer has a 50% chance of causing that error if I run the name server again within 30 seconds. Have you had this before? – Jeffersonjeffery