From 5834e38f14162c6ceb25f06106989912a41f7584 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Sun, 24 Jun 2018 11:39:50 +0200 Subject: [PATCH] Make run() accept a coroutine --- pyrogram/client/client.py | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/pyrogram/client/client.py b/pyrogram/client/client.py index 2025c524..e6080738 100644 --- a/pyrogram/client/client.py +++ b/pyrogram/client/client.py @@ -288,19 +288,25 @@ class Client(Methods, BaseClient): await self.stop() - def run(self): + def run(self, coroutine=None): """Use this method to automatically start and idle a Client. - Requires no parameters. + If a coroutine is passed as argument this method will start the client, run the coroutine + until is complete and then stop the client automatically. + + Args: + coroutine: (``Coroutine``, *optional*): + Pass a coroutine to run it until is complete. Raises: :class:`Error ` """ - asyncio.get_event_loop().run_until_complete( - asyncio.gather( - self.start(), - self.idle() - ) - ) + run = asyncio.get_event_loop().run_until_complete + + run(self.start()) + run(coroutine or self.idle()) + + if coroutine: + run(self.stop()) def add_handler(self, handler, group: int = 0): """Use this method to register an update handler.