Три способа выполнить множество задач с 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
#The_Economist🇬🇧📕[PDF]⬇️
15 #November2025
#Weekly_Magazines
For learning, for free(dom).
@backupofmagazines
This week’s Economist tracks how an emerging #AIBubble, volatile #Markets, and shifting #Geopolitics could reshape 2026. From Taiwan’s hidden economic risks to Mexico’s security crisis and Europe’s democratic strain, global fault lines widen. China faces birth-rate pressures and reputational challenges, while the U.S. wrestles with #Trumpforce, homeschooling, and e-bike parenting. Business pages explore #Chatbots, HR’s rise, and America’s “seven deadly sins.” Finance dives into “recession-recession” fears and personal-finance defence. Science highlights #AI superforecasting and whales’ vowel systems. A dense but essential issue for anyone tracking #Tech, #Policy, and global stability.
#The_Bloomberg🇺🇸📕[PDF]⬇️
#November2025
#Monthly_Magazines
For learning, for free(dom).
@backupofmagazines
This issue pits #GavinNewsom against a shifting #USPolitics map while probing the supposed #AIBubble, from frothy valuations to real-world productivity. A climate-forensics read asks whether #CloudSeeding worsened Dubai’s deadly storm—at the nexus of #ExtremeWeather and #ClimateChange. The geo-economics file tracks India’s wobbling investments, China’s expanding #Ports, and weaponized #SupplyChains. For investors, the issue weighs #ETFs, #401k tweaks, #BigTech concentration risk, and hedges in #Europe and #Gold. Lifestyle detours sample Utah powder and the dive-bar revival. It’s a brisk tour of power, money, and culture in a volatile world. #Markets#Investing#Energy#MiddleEast#Africa#Healthcare#Birkenstocks