Add new function compose

This commit is contained in:
Dan 2022-04-24 11:56:07 +02:00
parent 6fa4cdff15
commit 9be3818486
6 changed files with 76 additions and 6 deletions

View File

@ -339,7 +339,7 @@ def pyrogram_api():
f2.write(title + "\n" + "=" * len(title) + "\n\n") f2.write(title + "\n" + "=" * len(title) + "\n\n")
f2.write(".. automethod:: pyrogram.Client.{}()".format(method)) f2.write(".. automethod:: pyrogram.Client.{}()".format(method))
functions = ["idle"] functions = ["idle", "compose"]
for func in functions: for func in functions:
with open(root + "/{}.rst".format(func), "w") as f2: with open(root + "/{}.rst".format(func), "w") as f2:

View File

@ -1,8 +1,9 @@
Available Methods Available Methods
================= =================
This page is about Pyrogram methods. All the methods listed here are bound to a :class:`~pyrogram.Client` instance. This page is about Pyrogram methods. All the methods listed here are bound to a :class:`~pyrogram.Client` instance,
Some other utility functions can instead be found in the main package directly. except for :meth:`~pyrogram.idle()` and :meth:`~pyrogram.compose()`, which are special functions that can be found in
the main package directly.
.. code-block:: python .. code-block:: python
@ -40,11 +41,13 @@ Utilities
:nosignatures: :nosignatures:
idle idle
compose
.. toctree:: .. toctree::
:hidden: :hidden:
idle idle
compose
.. currentmodule:: pyrogram.Client .. currentmodule:: pyrogram.Client

View File

@ -37,6 +37,6 @@ class ContinuePropagation(StopAsyncIteration):
from . import raw, types, filters, handlers, emoji, enums from . import raw, types, filters, handlers, emoji, enums
from .client import Client from .client import Client
from .sync import idle from .sync import idle, compose
crypto_executor = ThreadPoolExecutor(1, thread_name_prefix="CryptoWorker") crypto_executor = ThreadPoolExecutor(1, thread_name_prefix="CryptoWorker")

View File

@ -0,0 +1,62 @@
# Pyrogram - Telegram MTProto API Client Library for Python
# Copyright (C) 2017-present Dan <https://github.com/delivrance>
#
# This file is part of Pyrogram.
#
# Pyrogram is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Pyrogram is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
from typing import List
import pyrogram
from .idle import idle
async def compose(clients: List["pyrogram.Client"]):
"""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.
If you want to run a single client, you can use Client's bound method :meth:`~pyrogram.Client.run`.
Parameters:
clients (List of :obj:`~pyrogram.Client`):
A list of client objects to run.
Example:
.. code-block:: python
import asyncio
from pyrogram import Client, compose
async def main():
app1 = Client("account1")
app2 = Client("account2")
app3 = Client("account3")
...
await compose([app1, app2, app3])
asyncio.run(main())
"""
for c in clients:
await c.start()
await idle()
for c in clients:
await c.stop()

View File

@ -38,6 +38,8 @@ class Run:
operation. This is almost the same as :py:obj:`asyncio.run` except for the fact that Pyrogram's ``run`` uses the operation. This is almost the same as :py:obj:`asyncio.run` except for the fact that Pyrogram's ``run`` uses the
current event loop instead of a new one. current event loop instead of a new one.
If you want to run multiple clients at once, see :meth:`pyrogram.compose`.
Parameters: Parameters:
coroutine (``Coroutine``, *optional*): coroutine (``Coroutine``, *optional*):
Pass a coroutine to run it until it completes. Pass a coroutine to run it until it completes.

View File

@ -23,7 +23,7 @@ import threading
from pyrogram import types from pyrogram import types
from pyrogram.methods import Methods from pyrogram.methods import Methods
from pyrogram.methods.utilities import idle as idle_module from pyrogram.methods.utilities import idle as idle_module, compose as compose_module
def async_to_sync(obj, name): def async_to_sync(obj, name):
@ -105,6 +105,9 @@ for class_name in dir(types):
if inspect.isclass(cls): if inspect.isclass(cls):
wrap(cls) wrap(cls)
# Special case for idle, because it's not inside Methods # Special case for idle and compose, because they are not inside Methods
async_to_sync(idle_module, "idle") async_to_sync(idle_module, "idle")
idle = getattr(idle_module, "idle") idle = getattr(idle_module, "idle")
async_to_sync(compose_module, "compose")
compose = getattr(compose_module, "compose")