Allow run_until_disconnected when the loop is running

This commit is contained in:
Lonami Exo 2018-06-25 13:32:31 +02:00
parent c1046c2acc
commit db5cb74bdd

View File

@ -16,6 +16,12 @@ class UpdateMethods(UserMethods):
# region Public methods # region Public methods
async def _run_until_disconnected(self):
try:
await self.disconnected
except KeyboardInterrupt:
await self.disconnect()
def run_until_disconnected(self): def run_until_disconnected(self):
""" """
Runs the event loop until `disconnect` is called or if an error Runs the event loop until `disconnect` is called or if an error
@ -23,14 +29,13 @@ class UpdateMethods(UserMethods):
the latter case, said error will ``raise`` so you have a chance the latter case, said error will ``raise`` so you have a chance
to ``except`` it on your own code. to ``except`` it on your own code.
This method shouldn't be called from ``async def`` as the loop If the loop is already running, this method returns a coroutine
will be running already. Use ``await client.disconnected`` in that you should await on your own code.
this situation instead.
""" """
try: if self.loop.is_running():
self.loop.run_until_complete(self.disconnected) return self._run_until_disconnected() # Let the user await it
except KeyboardInterrupt: else:
self.loop.run_until_complete(self.disconnect()) self.loop.run_until_complete(self._run_until_disconnected())
def on(self, event): def on(self, event):
""" """