Три способа выполнить множество задач с asyncio
Функция для примера:
async def do_it(n):
await asyncio.sleep(random.uniform(0.5, 1))
return n
1. Последовательный вызов
async def main():
for i in range(100):
result = await do_it(i)
Такой вызов имеет смысл только тогда, когда результат одной задачи требуется для вызова следующей.
Если они независимы, то это антипаттерн, так как аналогичен простому синхронному вызову по очереди.
2. Упорядоченный результат
async def main():
tasks = [do_it(i) for i in range(100)]
results = await asyncio.gather(*tasks)
Выполняет корутины конкурентно и возвращает результат в виде списка.
Полезен когда требуется получить результаты в том же порядке в котором задачи отправлены.
3. Результат по мере готовности
tasks = [asyncio.create_task(do_it(i)) for i in range(100)]
for cor in asyncio.as_completed(tasks):
result = await cor
Так же выполняет корутины конкурентно, но не гарантирует порядок. Результат возвращается по мере готовности, каждый отдельно.
Полезен когда нужно обработать любой ответ как можно скорее.
#async
🤖 Bot Downtime Update 🔧
Hey everyone,
Our bot is currently down due to a server-side issue from our hosting provider. This outage is not caused by us directly, but we’re in constant contact with the provider to ensure it gets resolved as quickly as possible.
We truly understand how frustrating this is — we’re equally affected and doing everything we can behind the scenes to bring the service back online.
🙏 Thanks for your patience and support.
We’ll keep you updated here. Please stay tuned.
#fixed✈️
Fixed an issue where @ChatNetworkBot in some cases didn't reply when it should send an invite link after the /start via deeplinking.
Thanks in helping diagnose, L.s. and C.
#fixed#ChatNetworkBot
🎈Hot Game This Week🎈
#Minecraft 1.21.92 LIVE!!
What's New?
🌟1.21.90 bugs #fixed!
Resolved a problem where adding split-screen players could disconnect users from multiplayer worlds.
🌟Visual Arts Adjusments!
Items with dimensions smaller than 16 pixels in width or height now display correctly once more.
🔗Minecraft 1.21.92 APK Patch Update
It was brought to attention that with some bots admin commands were no longer working.
That was happining for the bots migrated yesterday, because of the new bot API version.
This is #fixed now.
Thanks for letting me know, A. A.!
#BanWarnBot#RulesRulesBot#JoinCaptchaBot