Browse Source

Add back signal handling to Client.run

Apparently Python does not transform SIGTERM to KeyboardInterrupt as
nicely as I thought.
pull/2152/head
Rapptz 6 years ago
parent
commit
123e151978
  1. 18
      discord/client.py

18
discord/client.py

@ -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

Loading…
Cancel
Save