mirror of
https://github.com/Rapptz/discord.py.git
synced 2025-05-11 08:19:54 +00:00
Client.run now does cleanup when KeyboardInterrupt is encountered.
This commit is contained in:
parent
9137d92f67
commit
6d26d73e24
@ -262,8 +262,13 @@ class Client:
|
||||
def _run_event(self, event, *args, **kwargs):
|
||||
try:
|
||||
yield from getattr(self, event)(*args, **kwargs)
|
||||
except Exception as e:
|
||||
except asyncio.CancelledError:
|
||||
pass
|
||||
except Exception:
|
||||
try:
|
||||
yield from self.on_error(event, *args, **kwargs)
|
||||
except asyncio.CancelledError:
|
||||
pass
|
||||
|
||||
def dispatch(self, event, *args, **kwargs):
|
||||
log.debug('Dispatching event {}'.format(event))
|
||||
@ -278,6 +283,7 @@ class Client:
|
||||
|
||||
@asyncio.coroutine
|
||||
def keep_alive_handler(self, interval):
|
||||
try:
|
||||
while not self._closed:
|
||||
payload = {
|
||||
'op': 1,
|
||||
@ -288,6 +294,8 @@ class Client:
|
||||
log.debug(msg.format(payload['d']))
|
||||
yield from self.ws.send(utils.to_json(payload))
|
||||
yield from asyncio.sleep(interval)
|
||||
except asyncio.CancelledError:
|
||||
pass
|
||||
|
||||
@asyncio.coroutine
|
||||
def on_error(self, event_method, *args, **kwargs):
|
||||
@ -662,13 +670,33 @@ class Client:
|
||||
"""A blocking call that abstracts away the `event loop`_
|
||||
initialisation from you.
|
||||
|
||||
Equivalent to: ::
|
||||
If you want more control over the event loop then this
|
||||
function should not be used. Use :meth:`start` coroutine
|
||||
or :meth:`connect` + :meth:`login`.
|
||||
|
||||
Roughly Equivalent to: ::
|
||||
|
||||
try:
|
||||
loop.run_until_complete(start(email, password))
|
||||
except KeyboardInterrupt:
|
||||
loop.run_until_complete(logout())
|
||||
# cancel all tasks lingering
|
||||
finally:
|
||||
loop.close()
|
||||
"""
|
||||
|
||||
try:
|
||||
self.loop.run_until_complete(self.start(email, password))
|
||||
except KeyboardInterrupt:
|
||||
self.loop.run_until_complete(self.logout())
|
||||
pending = asyncio.Task.all_tasks()
|
||||
gathered = asyncio.gather(*pending)
|
||||
try:
|
||||
gathered.cancel()
|
||||
self.loop.run_forever()
|
||||
gathered.exception()
|
||||
except:
|
||||
pass
|
||||
finally:
|
||||
self.loop.close()
|
||||
|
||||
# event registration
|
||||
|
@ -175,6 +175,7 @@ class VoiceClient:
|
||||
|
||||
@asyncio.coroutine
|
||||
def keep_alive_handler(self, delay):
|
||||
try:
|
||||
while True:
|
||||
payload = {
|
||||
'op': 3,
|
||||
@ -185,6 +186,8 @@ class VoiceClient:
|
||||
log.debug(msg.format(payload['d']))
|
||||
yield from self.ws.send(utils.to_json(payload))
|
||||
yield from asyncio.sleep(delay)
|
||||
except asyncio.CancelledError:
|
||||
pass
|
||||
|
||||
@asyncio.coroutine
|
||||
def received_message(self, msg):
|
||||
|
Loading…
x
Reference in New Issue
Block a user