Better termination when exiting while connecting to the RPC daemon

This commit is contained in:
Anders Ingemann 2015-01-25 19:29:26 +01:00
parent 9505110d4a
commit 4582ea1498

View file

@ -103,12 +103,15 @@ def connect_pyro(host, port):
server_uri = 'PYRO:server@{host}:{port}'.format(host=host, port=port) server_uri = 'PYRO:server@{host}:{port}'.format(host=host, port=port)
connection = Pyro4.Proxy(server_uri) connection = Pyro4.Proxy(server_uri)
log.debug('Connecting to the RPC daemon') log.debug('Connecting to RPC daemon')
connected = False
try:
remaining_retries = 5 remaining_retries = 5
while True: while not connected:
try: try:
connection.ping() connection.ping()
break connected = True
except (Pyro4.errors.ConnectionClosedError, Pyro4.errors.CommunicationError): except (Pyro4.errors.ConnectionClosedError, Pyro4.errors.CommunicationError):
if remaining_retries > 0: if remaining_retries > 0:
remaining_retries -= 1 remaining_retries -= 1
@ -117,9 +120,11 @@ def connect_pyro(host, port):
else: else:
raise raise
try:
yield connection yield connection
finally: finally:
log.debug('Stopping the RPC daemon') if connected:
log.debug('Stopping RPC daemon')
connection.stop() connection.stop()
connection._pyroRelease() connection._pyroRelease()
else:
log.warn('Unable to stop RPC daemon, it might still be running on the server')