Три способа выполнить множество задач с 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
I sprayed her butt just a little with hand sanitizer because she keeps on biting wires on the desk.. I swear just a little 🥺 & she slowly sit there, lying down, with her eyes looking at me for a while until slowly falls asleep 🥺 she stopped biting the wires but I feel bad 🥺 Is she sad? or just tired? 🥺
#random#Henoa
@quietworld🍃
Today, a thought strucked me;
I love looking at cats especially high breed cat but I never wanted to buy and take care of high breed cat because I'm scared that I'm not able to take good care of it since high breed requires high maintenance.. So, I only wanted to take care of village cat, but then, I treat the village cat like high breed cat 😂
So.. why was I scared to adopt/buy high breed cat, right? I treat my village cat like royal anyway 😂
* currently waiting for my kitten to come back from home-based grooming service (waiting anxiously) *
I hope she comes back here safe 🥺😢
#thoughts#cat#Henoa
@quietworld🍃
Yeyy she's finally back from grooming 😍 Her nails clipped, ears cleaned and she smells good ☺❤ I'll get no more scars because her habit of climbing me 🤣 for now.. until her nails grow long and sharp again 🤣
Worth the wait..
from 12:30PM until 03:40PM🥰
#cat#Henoa#chupdate
@quietworld🍃
Shopping for Henoa le Kitten again! 😍
Unplanned but I found item that I didn't know exist before that I think Henoa & I needs and I read lots of reviews saying that this shop sells a lot cheaper than other stores so why not? 😂
Will be making a video of unboxing these + review once they arrived and tested 🥰💞
Stay tuned for this video upload in my channel 🥰
#marcegetaway#youtube#kitten#Henoa
@quietworld🍃
New video uploaded! 😍
p/s: not the video I mentioned in my sneak peek post recently, that is still in the making😉
#adoptingastraykittenjourney#adoptdontshop#Henoa#kitten#youtube#chupdate
@quietworld🌻
New video uploaded! 😍
p/s: not the video I mentioned in my sneak peek post recently, that is still in the making😉
#shopee#haul#Henoa#kitten#youtube#chupdate
@quietworld🌻