Re-raise exceptions from Client.start in Client.run.
This commit is contained in:
parent
f3eb039c4b
commit
6e2ecbc167
@ -454,17 +454,21 @@ class Client:
|
|||||||
|
|
||||||
|
|
||||||
def _do_cleanup(self):
|
def _do_cleanup(self):
|
||||||
if self.loop.is_closed():
|
loop = self.loop
|
||||||
|
if loop.is_closed():
|
||||||
return # we're already cleaning up
|
return # we're already cleaning up
|
||||||
|
|
||||||
self.loop.run_until_complete(self.close())
|
if loop.is_running():
|
||||||
pending = asyncio.Task.all_tasks(loop=self.loop)
|
loop.stop()
|
||||||
|
|
||||||
|
loop.run_until_complete(self.close())
|
||||||
|
pending = asyncio.Task.all_tasks(loop=loop)
|
||||||
if pending:
|
if pending:
|
||||||
log.info('Cleaning up after %s tasks', len(pending))
|
log.info('Cleaning up after %s tasks', len(pending))
|
||||||
gathered = asyncio.gather(*pending, loop=self.loop)
|
gathered = asyncio.gather(*pending, loop=loop)
|
||||||
try:
|
try:
|
||||||
gathered.cancel()
|
gathered.cancel()
|
||||||
self.loop.run_until_complete(gathered)
|
loop.run_until_complete(gathered)
|
||||||
|
|
||||||
# we want to retrieve any exceptions to make sure that
|
# we want to retrieve any exceptions to make sure that
|
||||||
# they don't nag us about it being un-retrieved.
|
# they don't nag us about it being un-retrieved.
|
||||||
@ -472,7 +476,7 @@ class Client:
|
|||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
self.loop.close()
|
loop.close()
|
||||||
|
|
||||||
def run(self, *args, **kwargs):
|
def run(self, *args, **kwargs):
|
||||||
"""A blocking call that abstracts away the `event loop`_
|
"""A blocking call that abstracts away the `event loop`_
|
||||||
@ -506,25 +510,24 @@ class Client:
|
|||||||
|
|
||||||
task = compat.create_task(self.start(*args, **kwargs), loop=loop)
|
task = compat.create_task(self.start(*args, **kwargs), loop=loop)
|
||||||
|
|
||||||
def kill_loop_on_finish(fut):
|
def stop_loop_on_finish(fut):
|
||||||
try:
|
|
||||||
fut.result()
|
|
||||||
except:
|
|
||||||
pass # don't care
|
|
||||||
finally:
|
|
||||||
loop.stop()
|
loop.stop()
|
||||||
|
|
||||||
task.add_done_callback(kill_loop_on_finish)
|
task.add_done_callback(stop_loop_on_finish)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
loop.run_forever()
|
loop.run_forever()
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
pass
|
pass
|
||||||
finally:
|
finally:
|
||||||
task.remove_done_callback(kill_loop_on_finish)
|
task.remove_done_callback(stop_loop_on_finish)
|
||||||
if is_windows:
|
if is_windows:
|
||||||
self._do_cleanup()
|
self._do_cleanup()
|
||||||
|
|
||||||
|
if task.cancelled():
|
||||||
|
return None
|
||||||
|
return task.result()
|
||||||
|
|
||||||
# properties
|
# properties
|
||||||
|
|
||||||
def is_closed(self):
|
def is_closed(self):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user