Skip to content

Commit 9be3818

Browse files
committed
Add new function compose
1 parent 6fa4cdf commit 9be3818

File tree

6 files changed

+76
-6
lines changed

6 files changed

+76
-6
lines changed

compiler/docs/compiler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ def get_title_list(s: str) -> list:
339339
f2.write(title + "\n" + "=" * len(title) + "\n\n")
340340
f2.write(".. automethod:: pyrogram.Client.{}()".format(method))
341341

342-
functions = ["idle"]
342+
functions = ["idle", "compose"]
343343

344344
for func in functions:
345345
with open(root + "/{}.rst".format(func), "w") as f2:

compiler/docs/template/methods.rst

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
Available Methods
22
=================
33

4-
This page is about Pyrogram methods. All the methods listed here are bound to a :class:`~pyrogram.Client` instance.
5-
Some other utility functions can instead be found in the main package directly.
4+
This page is about Pyrogram methods. All the methods listed here are bound to a :class:`~pyrogram.Client` instance,
5+
except for :meth:`~pyrogram.idle()` and :meth:`~pyrogram.compose()`, which are special functions that can be found in
6+
the main package directly.
67

78
.. code-block:: python
89
@@ -40,11 +41,13 @@ Utilities
4041
:nosignatures:
4142

4243
idle
44+
compose
4345

4446
.. toctree::
4547
:hidden:
4648

4749
idle
50+
compose
4851

4952
.. currentmodule:: pyrogram.Client
5053

pyrogram/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,6 @@ class ContinuePropagation(StopAsyncIteration):
3737

3838
from . import raw, types, filters, handlers, emoji, enums
3939
from .client import Client
40-
from .sync import idle
40+
from .sync import idle, compose
4141

4242
crypto_executor = ThreadPoolExecutor(1, thread_name_prefix="CryptoWorker")
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# Pyrogram - Telegram MTProto API Client Library for Python
2+
# Copyright (C) 2017-present Dan <https://github.com/delivrance>
3+
#
4+
# This file is part of Pyrogram.
5+
#
6+
# Pyrogram is free software: you can redistribute it and/or modify
7+
# it under the terms of the GNU Lesser General Public License as published
8+
# by the Free Software Foundation, either version 3 of the License, or
9+
# (at your option) any later version.
10+
#
11+
# Pyrogram is distributed in the hope that it will be useful,
12+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
# GNU Lesser General Public License for more details.
15+
#
16+
# You should have received a copy of the GNU Lesser General Public License
17+
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
18+
19+
from typing import List
20+
21+
import pyrogram
22+
from .idle import idle
23+
24+
25+
async def compose(clients: List["pyrogram.Client"]):
26+
"""Run multiple clients at once.
27+
28+
This method can be used to run multiple clients at once and can be found directly in the ``pyrogram`` package.
29+
30+
If you want to run a single client, you can use Client's bound method :meth:`~pyrogram.Client.run`.
31+
32+
Parameters:
33+
clients (List of :obj:`~pyrogram.Client`):
34+
A list of client objects to run.
35+
36+
Example:
37+
.. code-block:: python
38+
39+
import asyncio
40+
from pyrogram import Client, compose
41+
42+
43+
async def main():
44+
app1 = Client("account1")
45+
app2 = Client("account2")
46+
app3 = Client("account3")
47+
48+
...
49+
50+
await compose([app1, app2, app3])
51+
52+
53+
asyncio.run(main())
54+
55+
"""
56+
for c in clients:
57+
await c.start()
58+
59+
await idle()
60+
61+
for c in clients:
62+
await c.stop()

pyrogram/methods/utilities/run.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ def run(
3838
operation. This is almost the same as :py:obj:`asyncio.run` except for the fact that Pyrogram's ``run`` uses the
3939
current event loop instead of a new one.
4040
41+
If you want to run multiple clients at once, see :meth:`pyrogram.compose`.
42+
4143
Parameters:
4244
coroutine (``Coroutine``, *optional*):
4345
Pass a coroutine to run it until it completes.

pyrogram/sync.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
from pyrogram import types
2525
from pyrogram.methods import Methods
26-
from pyrogram.methods.utilities import idle as idle_module
26+
from pyrogram.methods.utilities import idle as idle_module, compose as compose_module
2727

2828

2929
def async_to_sync(obj, name):
@@ -105,6 +105,9 @@ def wrap(source):
105105
if inspect.isclass(cls):
106106
wrap(cls)
107107

108-
# Special case for idle, because it's not inside Methods
108+
# Special case for idle and compose, because they are not inside Methods
109109
async_to_sync(idle_module, "idle")
110110
idle = getattr(idle_module, "idle")
111+
112+
async_to_sync(compose_module, "compose")
113+
compose = getattr(compose_module, "compose")

0 commit comments

Comments
 (0)