Три способа выполнить множество задач с 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
Big news for Apple users! 🍏✨ Did you hear? Apple has finally allowed emulators on iOS, and Delta stands out as one of the best out there! 🎮 Have you given it a try yet? Share your thoughts below! #iOS#Emulators#Delta
🌍 The Mississippi River Delta’s vast freshwater wetlands act as a natural filter, removing pollutants and buffering storm surges. These shifting marshes shelter over 400 species of fish and wildlife. ✨
#wetlands⚡#delta⚡#biodiversity⚡#geography⚡#nature⚡#earth
👉subscribe Amazing Geography
👉more Channels
🌍 The Okavango Delta in southern Africa floods seasonally with rainwater from distant highlands, creating vast, ever-shifting freshwater wetlands that support hundreds of rare animal species. ✨
#wetlands⚡#delta⚡#biodiversity⚡#geography⚡#nature⚡#earth
👉subscribe Amazing Geography
👉more Channels
🌍 The Danube Delta in Eastern Europe contains over 30 distinct types of freshwater wetlands, making it one of the most biodiverse delta systems in the world outside the tropics. ✨
#wetlands⚡#delta⚡#biodiversity⚡#geography⚡#nature⚡#earth
👉subscribe Amazing Geography
👉more Channels
🌍 Ethiopia’s Lake Tana is the source of the Blue Nile and is surrounded by freshwater wetlands that support dozens of bird species found nowhere else, making it a hotspot for endemic wildlife. ✨
#wetlands⚡#delta⚡#biodiversity⚡#geography⚡#nature⚡#earth
👉subscribe Amazing Geography
👉more Channels
🌍 River deltas like Vietnam’s Mekong and the Mississippi in the US form complex freshwater wetlands, creating twisting channels and islands that shelter rare fish, birds, and vital crops. ✨
#wetlands⚡#delta⚡#biodiversity⚡#geography⚡#nature⚡#earth
👉subscribe Amazing Geography
👉more Channels
🌍 Wetlands in the Siberian taiga freeze solid each winter, yet come alive in spring to host millions of migrating birds. These seasonal deltas become some of the world’s richest breeding grounds. ✨
#wetlands⚡#delta⚡#migration⚡#geography⚡#nature⚡#earth
👉subscribe Amazing Geography🌍
🌍 The Okavango Delta in Botswana is one of the few major river deltas that forms entirely inland, creating a lush wetland oasis in the middle of the Kalahari Desert. ✨
#rivers⚡#wetlands⚡#delta⚡#geography⚡#nature⚡#earth
👉subscribe Amazing Geography🌍
🌍 The Sudd in South Sudan is the largest freshwater wetland in the Nile basin. During flood season, its floating reeds can form islands that drift for kilometers across the swamp’s surface. ✨
#wetlands⚡#delta⚡#Africa⚡#geography⚡#nature⚡#earth
👉subscribe Amazing Geography🌍
🌍 Some freshwater wetlands and deltas trap so much carbon in their soil that, hectare for hectare, they store more than most forests—making them powerful natural climate regulators. ✨
#wetlands⚡#delta⚡#carbon⚡#geography⚡#nature⚡#earth
👉subscribe Amazing Geography
👉more Channels
🌍 The Florida Everglades, a vast freshwater wetland, is the only place on Earth where alligators and crocodiles coexist in the wild, thanks to its unique mix of habitats and water conditions. ✨
#wetlands⚡#delta⚡#wildlife⚡#geography⚡#nature⚡#earth
👉subscribe Amazing Geography🌍