aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRapptz <[email protected]>2019-05-11 14:25:04 -0400
committerRapptz <[email protected]>2019-05-11 14:40:31 -0400
commit123e1519788d12559fafc030b3c97b8dccad38a6 (patch)
tree4f8adfaebde3e1d855e23caa8ec76bdfc391f718
parentDon't overwrite data parameter in webhooks. (diff)
downloaddiscord.py-123e1519788d12559fafc030b3c97b8dccad38a6.tar.xz
discord.py-123e1519788d12559fafc030b3c97b8dccad38a6.zip
Add back signal handling to Client.run
Apparently Python does not transform SIGTERM to KeyboardInterrupt as nicely as I thought.
-rw-r--r--discord/client.py18
1 files changed, 16 insertions, 2 deletions
diff --git a/discord/client.py b/discord/client.py
index ff87fdf5..d92e7c50 100644
--- a/discord/client.py
+++ b/discord/client.py
@@ -534,19 +534,33 @@ class Client:
is blocking. That means that registration of events or anything being
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():
try:
await self.start(*args, **kwargs)
finally:
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:
- self.loop.run_until_complete(runner())
+ loop.run_forever()
except KeyboardInterrupt:
log.info('Received signal to terminate bot and event loop.')
finally:
+ future.remove_done_callback(stop_loop_on_completion)
log.info('Cleaning up tasks.')
- _cleanup_loop(self.loop)
+ _cleanup_loop(loop)
# properties