Skip to content

Commit dc3b8a5

Browse files
committed
Tweak file upload settings
Multiple sessions as used in the current implementation were causing a variety of network related issues. Use one session only instead. Multiple workers within the same session are fine as long as they are not too many, otherwise the server will start replying with -429 (too many requests). Setting the queue size to 1 helps in having a more linear upload progress.
1 parent 9bf742a commit dc3b8a5

File tree

1 file changed

+8
-13
lines changed

1 file changed

+8
-13
lines changed

pyrogram/methods/advanced/save_file.py

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -134,23 +134,19 @@ async def worker(session):
134134

135135
file_total_parts = int(math.ceil(file_size / part_size))
136136
is_big = file_size > 10 * 1024 * 1024
137-
pool_size = 3 if is_big else 1
138137
workers_count = 4 if is_big else 1
139138
is_missing_part = file_id is not None
140139
file_id = file_id or self.rnd_id()
141140
md5_sum = md5() if not is_big and not is_missing_part else None
142-
pool = [
143-
Session(
144-
self, await self.storage.dc_id(), await self.storage.auth_key(),
145-
await self.storage.test_mode(), is_media=True
146-
) for _ in range(pool_size)
147-
]
148-
workers = [self.loop.create_task(worker(session)) for session in pool for _ in range(workers_count)]
149-
queue = asyncio.Queue(16)
141+
session = Session(
142+
self, await self.storage.dc_id(), await self.storage.auth_key(),
143+
await self.storage.test_mode(), is_media=True
144+
)
145+
workers = [self.loop.create_task(worker(session)) for _ in range(workers_count)]
146+
queue = asyncio.Queue(1)
150147

151148
try:
152-
for session in pool:
153-
await session.start()
149+
await session.start()
154150

155151
fp.seek(part_size * file_part)
156152

@@ -223,8 +219,7 @@ async def worker(session):
223219

224220
await asyncio.gather(*workers)
225221

226-
for session in pool:
227-
await session.stop()
222+
await session.stop()
228223

229224
if isinstance(path, (str, PurePath)):
230225
fp.close()

0 commit comments

Comments
 (0)