Три способа выполнить множество задач с 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
Pica is an eating disorder where people crave and eat non-food items like dirt, hair, metal, or chalk. It can be linked to nutritional deficiencies or mental health issues. Pica is a compulsive behavior that can lead to health problems but is treatable with medical care.
🍽️🧠🍴🪴🧱
[read more 1]
[read more 2]
@googlefactss
#Pica#EatingDisorder#MentalHealth#HealthFacts#UnusualEating
Michel Lotito, known as "Monsieur Mangetout," ate a Cessna 150 plane over two years. Due to his condition Pica, he craved non-food items like metal, rubber, and glass. Lotito started eating the plane in 1978, chewing through everything from the wings to the engine. He also consumed bicycles, shopping carts, and televisions. Lotito passed away in 2007 at the age of 57. His feats earned him multiple spots in the Guinness World Records.
👨🍳🛩️🛠️🔩🚲
[Read more]
@googlefactss
#MichelLotito#MonsieurMangetout#WorldRecords#EatingMetal#Pica#UnusualDiet#Guinness