Add back signal handling to Client.run

Apparently Python does not transform SIGTERM to KeyboardInterrupt as
nicely as I thought.
This commit is contained in:
Rapptz 2019-05-11 14:25:04 -04:00
parent 8206982e79
commit 123e151978

View File

@ -534,19 +534,33 @@ class Client:
is blocking. That means that registration of events or anything being is blocking. That means that registration of events or anything being
called after this function call will not execute until it returns. called after this function call will not execute until it returns.
""" """
loop = self.loop
try:
loop.add_signal_handler(signal.SIGINT, lambda: loop.stop())
loop.add_signal_handler(signal.SIGTERM, lambda: loop.stop())
except NotImplementedError:
pass
async def runner(): async def runner():
try: try:
await self.start(*args, **kwargs) await self.start(*args, **kwargs)
finally: finally:
await self.close() await self.close()
def stop_loop_on_completion(f):
loop.stop()
future = asyncio.ensure_future(runner(), loop=loop)
future.add_done_callback(stop_loop_on_completion)
try: try:
self.loop.run_until_complete(runner()) loop.run_forever()
except KeyboardInterrupt: except KeyboardInterrupt:
log.info('Received signal to terminate bot and event loop.') log.info('Received signal to terminate bot and event loop.')
finally: finally:
future.remove_done_callback(stop_loop_on_completion)
log.info('Cleaning up tasks.') log.info('Cleaning up tasks.')
_cleanup_loop(self.loop) _cleanup_loop(loop)
# properties # properties