Три способа выполнить множество задач с 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
⚡️JetTon Games: Exciting New Slots Arrived!
#JetTon#slots
JetTon Games has introduced new enticing slots including Starshine Crystals, Coin Express, and Fighter Pit, each offering unique features and high potential wins. Players can enjoy wilds, risk games, and bonus fights with impressive multipliers. Join the fun at JetTon Games and share your favorites in the comments!
Source: link
@tonlines
🔥1 week after Dragon's Call release and we got:
👾70,000players played
😮x1268max win
In celebration of this we redesign game cover in different art styles🎨, try to guess what those styles are!
#dragonscall#truelab#slots
Our new column - Panda news from Dragon's Call📰
Today we discuss Colombia's tax hike in gambling💔
What do you think of this format?🐼
#news#pandanews#slots#dragonscall
The World of Games Awaits
🎰Polistar Casino invites you to an exciting world of games! 🃏
From classics to the latest releases – our collection is designed to deliver vivid emotions and engaging leisure. Enjoy high-quality graphics and a user-friendly interface.
➡️ Explore the Polistar universe. Simple sign-up here:
[Polistar Casino]
#Sponsorship#CasinoPolistar#CasinoGames#Slots#NewCasino