From f6283757e14fce1d2edd19dd8bd29b7b73e6b3de Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Sun, 15 May 2022 14:26:12 +0200 Subject: [PATCH] Add sequential parameter to compose() --- pyrogram/methods/utilities/compose.py | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/pyrogram/methods/utilities/compose.py b/pyrogram/methods/utilities/compose.py index c8bdf769..cfc5ca3e 100644 --- a/pyrogram/methods/utilities/compose.py +++ b/pyrogram/methods/utilities/compose.py @@ -16,13 +16,17 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +import asyncio from typing import List import pyrogram from .idle import idle -async def compose(clients: List["pyrogram.Client"]): +async def compose( + clients: List["pyrogram.Client"], + sequential: bool = False +): """Run multiple clients at once. This method can be used to run multiple clients at once and can be found directly in the ``pyrogram`` package. @@ -33,6 +37,10 @@ async def compose(clients: List["pyrogram.Client"]): clients (List of :obj:`~pyrogram.Client`): A list of client objects to run. + sequential (``bool``, *optional*): + Pass True to run clients sequentially. + Defaults to False (run clients concurrently) + Example: .. code-block:: python @@ -53,10 +61,16 @@ async def compose(clients: List["pyrogram.Client"]): asyncio.run(main()) """ - for c in clients: - await c.start() + if sequential: + for c in clients: + await c.start() + else: + await asyncio.gather(*[c.start() for c in clients]) await idle() - for c in clients: - await c.stop() + if sequential: + for c in clients: + await c.stop() + else: + await asyncio.gather(*[c.stop() for c in clients])