Три способа выполнить множество задач с 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
🏝Un paysage marin saisissant à Tung Ping Chau, à #Hong Kong, avec ses rares falaises sédimentaires et ses côtes sculptées par les vagues, attire des visiteurs
Les fines roches laminées de l'île se sont formées à partir de limon et de boue déposés dans un lac salé il y a environ 55 millions d'années. Elle abrite également plus de 60 espèces de coraux, ce qui en fait l'un des paysages géologiques les plus insolites de Hong Kong.
RT en français • Osez questionner !
#Canadian miner Allied Gold, operating in #Ethiopia, sold to China’s Zijin Gold in $4bn deal
Canadian gold mining company #Allied_Gold, which operates major projects in Ethiopia, has agreed to be acquired by #China’s #Zijin_Gold International in an all-cash deal valued at about $4 billion, according to a report by the Financial Times.
The transaction will see #Hong Kong–listed Zijin Gold International acquire Allied Gold for C$5.5 billion ($4 billion), as record-high gold prices continue to drive consolidation across the global mining sector, the Financial Times reported.
Allied Gold has mining operations in Ethiopia, Mali, and Côte d’Ivoire. The company welcomed the deal, which values its shares at C$44 apiece — a 27 percent premium to its 30-day average share price prior to the announcement. The surge reflects strong investor appetite for gold assets amid soaring bullion prices......
https://www.facebook.com/AddisstandardEng/posts/pfbid05VJM2KTLGsAio8Py1W1QkitgusnwrVRLfN489qhATq8qGYuLDA6CmsHam7XQY3t7l