mirror of
https://github.com/alfem/telegram-download-daemon.git
synced 2024-09-20 14:56:06 +08:00
Limit the amount of parallel downloads to the amount of CPU cores
Resolves: #11
This commit is contained in:
parent
c65c893f5c
commit
4561be6f7a
|
@ -14,6 +14,7 @@ import logging
|
|||
logging.basicConfig(format='[%(levelname) 5s/%(asctime)s]%(name)s:%(message)s',
|
||||
level=logging.WARNING)
|
||||
|
||||
import multiprocessing
|
||||
import argparse
|
||||
import asyncio
|
||||
|
||||
|
@ -61,6 +62,7 @@ api_id = args.api_id
|
|||
api_hash = args.api_hash
|
||||
channel_id = args.channel
|
||||
downloadFolder = args.dest
|
||||
worker_count = multiprocessing.cpu_count()
|
||||
|
||||
# Edit these lines:
|
||||
proxy = None
|
||||
|
@ -82,6 +84,7 @@ with TelegramClient(getSession(), api_id, api_hash,
|
|||
|
||||
saveSession(client.session)
|
||||
|
||||
queue = asyncio.Queue()
|
||||
peerChannel = PeerChannel(channel_id)
|
||||
|
||||
@client.on(events.NewMessage())
|
||||
|
@ -93,6 +96,12 @@ with TelegramClient(getSession(), api_id, api_hash,
|
|||
print(event)
|
||||
|
||||
if event.media:
|
||||
queue.put_nowait(event)
|
||||
|
||||
async def worker():
|
||||
while True:
|
||||
event = await queue.get()
|
||||
|
||||
filename=event.media.document.attributes[0].file_name
|
||||
await log_reply(
|
||||
event,
|
||||
|
@ -101,9 +110,18 @@ with TelegramClient(getSession(), api_id, api_hash,
|
|||
|
||||
await client.download_media(event.message, downloadFolder)
|
||||
await log_reply(event, f"{filename} ready")
|
||||
|
||||
|
||||
queue.task_done()
|
||||
|
||||
async def start():
|
||||
tasks = []
|
||||
for i in range(worker_count):
|
||||
task = asyncio.create_task(worker())
|
||||
tasks.append(task)
|
||||
await sendHelloMessage(client, peerChannel)
|
||||
await client.run_until_disconnected()
|
||||
for task in tasks:
|
||||
task.cancel()
|
||||
await asyncio.gather(*tasks, return_exceptions=True)
|
||||
|
||||
client.loop.run_until_complete(start())
|
||||
|
|
Loading…
Reference in a new issue