i made a python bot that receive cmds from users and perfom the task . i am taking multiple ids ( as example ) and sending them to an api and result is storing and then sending the results to the user .
i am not using for loops cause its too slow i am using thread pool executor . for every user i gave max_workers = 5 .
when 1-2 people trying that commands its working perfectly . but when 50-80 users trying that commands they always getting "Checking..." and then no further improvement .
( the api i am sending request is perfectly fine it has no issue . it return the results in 1-1.5s even if you are making 10k requets at a time . )
this is my code . have a look . when someone send cmd it executed like this
from pyrogram import Client, filters
import requests
import concurrent.futures
import threading
import asyncio
import concurrent.futures
def do_work(id):
while True:
result = requests.get(f"example.com/api.php?id={id}")
if result.status_code == 200:
return result.text
else:
continue
@Client.on_message(filters.command("getresult", [".", "/"]))
def multi(Client, message):
t1 = threading.Thread(target=bcall, args=(Client, message))
t1.start()
def bcall(Client, message):
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(thread(Client, message))
loop.close()
async def thread(Client, message):
ids = message.text.split("\n")
ALL_RESULT = ""
await message.reply_text("Checking...")
with concurrent.futures.ThreadPoolExecutor(max_workers=10) as executor:
futures = []
for i in ids:
future = executor.submit(do_work , i)
futures.append(future)
for future in concurrent.futures.as_completed(futures):
result = future.result()
ALL_RESULT += result + "\n"
await message.reply_text(ALL_RESULT)
await message.reply_text(f"Done Checking {len(ids)} IDS")
( while loop ends in 2-3 sec . also i am using max_retry = 2) to the while loop then it breaks ) and when 50-80 user send cmd they always stuck at "Checking..." message . maybe the threads created are in waiting or something i dont know but i checked my cpu usage and its only using 25-30% . i am frustrated please any one help me fix this .
Your approach was too complicated. Here's a fully asynchronous way to achieve the same result. Explanation is given after the code.
Note: Bot token is revoked. So no worries.
instead of using
requests, I usedhttpx. So I can make asynchronous http requests.And we kept a count of errors in the while loop. So, the loop will not continue for more than 3 times.
And in the
getResultsfunction, I usedasyncio.gatherto retrieve the results concurrently. As you were usingmax_workers=10, so I also implemented it by thepervariable and a loop. So, the CPU will have less pressure. You can modify thepervariable as your need.Let me know if this solved your problem.