Три способа выполнить множество задач с 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
🔥 OGCommunity App for daily activity and grabbing OGC tokens for free and many more.
👉 Join Bot for Airdrop :- https://t.me/OGCommunityBot?start=i09w6Cfm9nz7V5wMcLjJVJpz
➖ Click on bot link
➖ Click on open app
➖ Complete tasks to get instant OGC tokens
➖ Mine every day & Done
#Note :- Join fast before #May 31st 🔥
Wikipedia每日一图(#May 16, 2026)https://ift.tt/ixodInV
Partially frozen Gurudongmar Lake, a glacial lake located to the north of the Himalayas in the northeast Indian state of Sikkim at an altitude of over 5,150 metres (16,900 ft). The lake is fed by glaciers of the Khangchengyao massif, forms the headwaters of the Teesta river and is considered sacred by Buddhists and Sikhs. Today is Sikkim Day, which commemorates the formation of Sikkim as a state of India in 1975, following a popular referendum and full merger after decades of being a protectorate since 1947.. 了解更多 . #Wikipedia每日一图
Wikipedia每日一图(#May 14, 2026)https://ift.tt/TXQ67sq
This stained glass window in the Cathedral of Saint Julian of Le Mans (Le Mans, France) depicts the Virgin Mary and the apostles during the Ascension of Jesus. Today is the Feast of the Ascension in Western Christianity.. 了解更多 . #Wikipedia每日一图
Wikipedia每日一图(#May 13, 2026)https://ift.tt/UjxK9Pg
This azulejo from the Igreja de São Bento (Ribeira Brava, Madeira, Portugal) depicts Our Lady of Fátima. Today is the feast of Our Lady of Fátima in the Catholic Church and the 110th anniversary of her first apparition to three shepherd children.. 了解更多 . #Wikipedia每日一图
Wikipedia每日一图(#May 12, 2026)https://ift.tt/EpzY47y
Aerial view of the ruins of Takht-i-Bahi, a 1st-century CE Buddhist monastery complex located in what was once the ancient Indian region of Gandhara, in the present-day northern Pakistani province of Khyber Pakhtunkhwa. It is representative of Buddhist monastic architecture from its era and the ruins were listed as a World Heritage Site in 1980, with UNESCO describing it as having been "exceptionally well-preserved".. 了解更多 . #Wikipedia每日一图
Wikipedia每日一图(#May 10, 2026)https://ift.tt/82UdYl5
Flower buds in development of a Crocosmia 'Lucifer' (Montbretia). Focus stack of 54 photos.. 了解更多 . #Wikipedia每日一图