From db5cb74bdd3c2b27d10c47163e3f038d1a7a1fdb Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Mon, 25 Jun 2018 13:32:31 +0200 Subject: [PATCH] Allow run_until_disconnected when the loop is running --- telethon/client/updates.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/telethon/client/updates.py b/telethon/client/updates.py index 6c303932..c7c5a4ac 100644 --- a/telethon/client/updates.py +++ b/telethon/client/updates.py @@ -16,6 +16,12 @@ class UpdateMethods(UserMethods): # region Public methods + async def _run_until_disconnected(self): + try: + await self.disconnected + except KeyboardInterrupt: + await self.disconnect() + def run_until_disconnected(self): """ 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 to ``except`` it on your own code. - This method shouldn't be called from ``async def`` as the loop - will be running already. Use ``await client.disconnected`` in - this situation instead. + If the loop is already running, this method returns a coroutine + that you should await on your own code. """ - try: - self.loop.run_until_complete(self.disconnected) - except KeyboardInterrupt: - self.loop.run_until_complete(self.disconnect()) + if self.loop.is_running(): + return self._run_until_disconnected() # Let the user await it + else: + self.loop.run_until_complete(self._run_until_disconnected()) def on(self, event): """