From 09463f491dc6b81bccff083193eb8c31e0851984 Mon Sep 17 00:00:00 2001 From: Egor3f Date: Sat, 18 Jul 2020 20:05:46 +0300 Subject: [PATCH] Parallel downloading and writing to stdout --- telecup_cli.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/telecup_cli.py b/telecup_cli.py index b5d93f3..5aa35a3 100644 --- a/telecup_cli.py +++ b/telecup_cli.py @@ -221,18 +221,28 @@ async def download_part(client: TelegramClient, dest: typing.BinaryIO, dInfo: Do totalBytesDownloaded = 0 lastRealTimeMeasurement = time.time() + buffer = None + currentLoop = asyncio.get_running_loop() + currentExecutor = ThreadPoolExecutor(max_workers=1) + + def writeBuffer(buf): + if buf: + dest.write(buf) + while totalBytesDownloaded < realSize: offset = totalBytesDownloaded limit = chunkSize try: - downloadResult: File = await client(GetFileRequest( + taskReceive = client(GetFileRequest( inputFileLocation, offset, limit, precise=False, cdn_supported=False )) + taskWriteBuffer = currentLoop.run_in_executor(currentExecutor, writeBuffer, buffer) + downloadResult, writeResult = await asyncio.gather(taskReceive, taskWriteBuffer, loop=currentLoop, return_exceptions=False) except BadRequestError as e: if 'expire' in str(e.message).lower(): click.echo('Reloading message...', err=True) @@ -249,7 +259,6 @@ async def download_part(client: TelegramClient, dest: typing.BinaryIO, dInfo: Do extraBytes = totalBytesDownloaded - realSize buffer = buffer[:-extraBytes] - dest.write(buffer) if totalBytesDownloaded % (pow(2, 20) * 10) == 0: click.echo( f"{dInfo.part_info['part']}p {format_file_size(totalBytesDownloaded)}b {(time.time() - lastRealTimeMeasurement):.3f}s\t",