Skip to content
This repository was archived by the owner on Dec 23, 2024. It is now read-only.

Commit 82b8c77

Browse files
committed
Allow to specify a limit to concurrent transmissions
1 parent 2a7110e commit 82b8c77

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

pyrogram/client.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,11 @@ class Client(Methods):
172172
Pass True to hide the password when typing it during the login.
173173
Defaults to False, because ``getpass`` (the library used) is known to be problematic in some
174174
terminal environments.
175+
176+
max_concurrent_transmissions (``bool``, *optional*):
177+
Set the maximum amount of concurrent transmissions (uploads & downloads).
178+
A value that is too high may result in network related issues.
179+
Defaults to 1.
175180
"""
176181

177182
APP_VERSION = f"Pyrogram {__version__}"
@@ -189,6 +194,8 @@ class Client(Methods):
189194
# Interval of seconds in which the updates watchdog will kick in
190195
UPDATES_WATCHDOG_INTERVAL = 5 * 60
191196

197+
MAX_CONCURRENT_TRANSMISSIONS = 1
198+
192199
mimetypes = MimeTypes()
193200
mimetypes.readfp(StringIO(mime_types))
194201

@@ -217,7 +224,8 @@ def __init__(
217224
no_updates: bool = None,
218225
takeout: bool = None,
219226
sleep_threshold: int = Session.SLEEP_THRESHOLD,
220-
hide_password: bool = False
227+
hide_password: bool = False,
228+
max_concurrent_transmissions: int = MAX_CONCURRENT_TRANSMISSIONS
221229
):
222230
super().__init__()
223231

@@ -245,6 +253,7 @@ def __init__(
245253
self.takeout = takeout
246254
self.sleep_threshold = sleep_threshold
247255
self.hide_password = hide_password
256+
self.max_concurrent_transmissions = max_concurrent_transmissions
248257

249258
self.executor = ThreadPoolExecutor(self.workers, thread_name_prefix="Handler")
250259

@@ -266,8 +275,8 @@ def __init__(
266275
self.media_sessions = {}
267276
self.media_sessions_lock = asyncio.Lock()
268277

269-
self.save_file_lock = asyncio.Lock()
270-
self.get_file_lock = asyncio.Lock()
278+
self.save_file_semaphore = asyncio.Semaphore(self.max_concurrent_transmissions)
279+
self.get_file_semaphore = asyncio.Semaphore(self.max_concurrent_transmissions)
271280

272281
self.is_connected = None
273282
self.is_initialized = None
@@ -798,7 +807,7 @@ async def get_file(
798807
progress: Callable = None,
799808
progress_args: tuple = ()
800809
) -> Optional[AsyncGenerator[bytes, None]]:
801-
async with self.get_file_lock:
810+
async with self.get_file_semaphore:
802811
file_type = file_id.file_type
803812

804813
if file_type == FileType.CHAT_PHOTO:

pyrogram/methods/advanced/save_file.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ async def save_file(
9494
Raises:
9595
RPCError: In case of a Telegram RPC error.
9696
"""
97-
async with self.save_file_lock:
97+
async with self.save_file_semaphore:
9898
if path is None:
9999
return None
100100

0 commit comments

Comments
 (0)