Три способа выполнить множество задач с 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
📰 NEWS | Maduro Confirms Sale of Venezuelan Fertilizer Firm Monómeros to Colombia, Ending Privatization Rumors
Venezuelan and Colombian authorities are engaged in talks for the sale of agrochemical producer Monómeros. A confidential agreement reportedly set a base price.
The firm, which had a tumultuous time under Venezuelan opposition management, is set to be purchased by the Colombian state as the Petro government aims to maintain stability in the agricultural inputs market.
Read our report: https://venezuelanalysis.com/news/maduro-confirms-sale-of-venezuelan-fertilizer-firm-monomeros-to-colombia-ending-privatization-rumors/
#Monomeros#Fertilizer#Venezuela#GustavoPetro
In-depth Analysis: The #Fertilizer Paradox: Global shocks test #Ethiopia’s fragile, corruption-tainted supply chain
As Ethiopia approaches a critical agricultural season, mounting global disruptions, domestic corruption, and logistical bottlenecks induced by #fuel_shortages are converging to expose deep vulnerabilities in the country’s #fertilizer_supply chain.
While officials maintain that sufficient fertilizer has been secured for the current production cycle, contradictory statements from senior policymakers, corruption revelations, fuel shortage and reports of scarcity from farming communities suggest a widening gap between national preparedness and realities on the ground.
The emerging picture is one of a system already under strain, shaped by global geopolitical shocks abroad and persistent structural weaknesses at home.
Read the full In-depth Analysis:https://addisstandard.com/?p=56490