Три способа выполнить множество задач с 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
Highest quality soft crates from Sweden 🇸🇪 not China 🇨🇳 junk and not excessive cost ,
the usual 5 sizes are available now and on stock…
60cm 59€
70cm 69€
80cm 80€
105cm 95€
120cm 110€
Available at the following upcoming big CACIB shows
Tallinn Estonia 🇪🇪 August
Palanga Lithuania 🇱🇹 September
Komarom Hungary 🇭🇺 October
…Or
you can order online at Kennel.Ai
…Register your new puppies or pet dogs for additional discounts
https://kennel.ai/login
#$dogs #$bone #petdognft#decentralised
Have a look at the Kennel.ai website blog to learn more about dogs
https://kennel.ai/blogs
Also watch our YouTube channel
https://www.youtube.com/@Dogstoken
#doghealth#dogblog#petdogNFT #$DOGS
Have a look at the Kennel.ai website blog to learn more about dogs
https://kennel.ai/blogs
Also watch our YouTube channel
https://www.youtube.com/@Dogstoken
#doghealth#dogblog#petdogNFT #$DOGS
Have a look at the Kennel.ai website blog to learn more about dogs
https://kennel.ai/blogs
Also watch our YouTube channel
https://www.youtube.com/@Dogstoken
#doghealth#dogblog#petdogNFT #$DOGS
Have a look at the Kennel.ai website blog to learn more about dogs
https://kennel.ai/blogs
Also watch our YouTube channel
https://www.youtube.com/@Dogstoken
#doghealth#dogblog#petdogNFT #$DOGS
Direct from UK manufacturer
Heavyweight rubber back VetBed
Buy now before the Winter ⛄️
Warm , Machine Washable 50 colour choices
Buy full roll for maximum discount
Free home delivery ;
Estonia 🇪🇪 Finland 🇫🇮 Latvia 🇱🇻
Lithuania 🇱🇹 Poland 🇵🇱 Slovakia 🇸🇰
Czech Republic 🇨🇿 Hungary 🇭🇺
Croatia 🇭🇷
Payment by bank crypto or to the delivery 📦 driver 🚚
www.Kennel.Ai
#VetBed#Kennel.Ai #DogBreeder#PetDogNFT
Direct from UK manufacturer
Heavyweight rubber back VetBed
Buy now before the Winter ⛄️
Warm , Machine Washable 50 colour choices
Buy full roll for maximum discount
Free home delivery ;
Estonia 🇪🇪 Finland 🇫🇮 Latvia 🇱🇻
Lithuania 🇱🇹 Poland 🇵🇱 Slovakia 🇸🇰
Czech Republic 🇨🇿 Hungary 🇭🇺
Croatia 🇭🇷
Payment by bank crypto or to the delivery 📦 driver 🚚
www.Kennel.Ai
#VetBed#Kennel.Ai #DogBreeder#PetDogNFT
Direct from UK manufacturer
Heavyweight rubber back VetBed
Buy now before the Winter ⛄️
Warm , Machine Washable 50 colour choices
Buy full roll for maximum discount
Free home delivery ;
Estonia 🇪🇪 Finland 🇫🇮 Latvia 🇱🇻
Lithuania 🇱🇹 Poland 🇵🇱 Slovakia 🇸🇰
Czech Republic 🇨🇿 Hungary 🇭🇺
Croatia 🇭🇷
Payment by bank crypto or to the delivery 📦 driver 🚚
www.Kennel.Ai
#VetBed#Kennel.Ai #DogBreeder#PetDogNFT