diff --git a/compiler/docs/compiler.py b/compiler/docs/compiler.py index f387d515..821c3f95 100644 --- a/compiler/docs/compiler.py +++ b/compiler/docs/compiler.py @@ -139,6 +139,7 @@ def pyrogram_api(): start stop run + run_sync restart add_handler remove_handler diff --git a/pyrogram/methods/utilities/__init__.py b/pyrogram/methods/utilities/__init__.py index 80a5f741..e34fc7a3 100644 --- a/pyrogram/methods/utilities/__init__.py +++ b/pyrogram/methods/utilities/__init__.py @@ -21,6 +21,7 @@ from .export_session_string import ExportSessionString from .remove_handler import RemoveHandler from .restart import Restart from .run import Run +from .run_sync import RunSync from .start import Start from .stop import Stop from .stop_transmission import StopTransmission @@ -32,6 +33,7 @@ class Utilities( RemoveHandler, Restart, Run, + RunSync, Start, Stop, StopTransmission diff --git a/pyrogram/methods/utilities/run_sync.py b/pyrogram/methods/utilities/run_sync.py new file mode 100755 index 00000000..864321ec --- /dev/null +++ b/pyrogram/methods/utilities/run_sync.py @@ -0,0 +1,41 @@ +"""PyroFork async utils""" +# Copyright (C) 2020 - 2023 UserbotIndo Team, +# Copyright (C) 2022-present Mayuri-Chan, +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program 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 General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +from pyrogram import utils +from typing import Any, Callable, TypeVar + +class RunSync: + Result = TypeVar("Result") + + async def run_sync(self, func: Callable[..., Result], *args: Any, **kwargs: Any) -> Result: + """Runs the given sync function (optionally with arguments) on a separate thread. + + Parameters: + func (``Callable``): + Sync function to run. + + \\*args (``any``, *optional*): + Function argument. + + \\*\\*kwargs (``any``, *optional*): + Function extras arguments. + + Returns: + ``any``: The function result. + """ + + return await utils.run_sync(func, *args, **kwargs) diff --git a/pyrogram/utils.py b/pyrogram/utils.py index 3918e559..79a66259 100644 --- a/pyrogram/utils.py +++ b/pyrogram/utils.py @@ -26,7 +26,7 @@ import struct from concurrent.futures.thread import ThreadPoolExecutor from datetime import datetime, timezone from getpass import getpass -from typing import Union, List, Dict, Optional +from typing import Union, List, Dict, Optional, Any, Callable, TypeVar import pyrogram from pyrogram import raw, enums @@ -374,3 +374,7 @@ def timestamp_to_datetime(ts: Optional[int]) -> Optional[datetime]: def datetime_to_timestamp(dt: Optional[datetime]) -> Optional[int]: return int(dt.timestamp()) if dt else None + +async def run_sync(func: Callable[..., TypeVar("Result")], *args: Any, **kwargs: Any) -> TypeVar("Result"): + loop = asyncio.get_event_loop() + return await loop.run_in_executor(None, functools.partial(func, *args, **kwargs))