Три способа выполнить множество задач с 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
💨 Nothing on the road will scare you after this
🇸🇪 In Sweden, driving schools teach students how to handle a car in all kinds of tricky conditions.
🚗 And yes — passing a skid-control test is mandatory before they can even get their license.
#driving@Car_News
🚗🇺🇸1 IN 3 US PARENTS FEAR TEEN DRIVERS WILL CAUSE DEADLY CRASH
🔹 University of Michigan poll: 33% worry their young driver could cause accident — but 96% think their kid drives "average or better" 🤔📊
🔹 Half (51%) witnessed unsafe behavior: 44% saw aggressive driving, 25% caught texting while driving, 17% saw impaired/sleepy driving 😱🚨
🔹 Only 24% imposed consequences — most just take keys (6%), refuse car (8%), or threaten insurance cuts (6%) 🔑💳
🔹 Parents who saw road rage LESS likely to rate teen as bad driver — massive blind spot putting lives at risk 🙈💀
American teens NOT ready for the road. Where are the parents? 😬🛣️
#USNews#teensafety#driving
@america
J-j家j谱p- 家谱 The Family Tree (2011)
直达链接:https://pan.quark.cn/s/32c4d8126f28
#家谱#The Family Tree #Driving Lessons
链接:https://link3.cc/sf_com
#电影#喜剧#美国#10年代
🔥🏁Koenigsegg Regera Breaks Rimac Nevera's Record in 0-400-0 Challenge 🏁🔥
🏆 The music didn't play for long: Rimac Nevera's record in the 0-400-0 km/h acceleration-braking challenge lasted only a month. The Croatian hypercar surpassed the previous record holder, Koenigsegg Regera, clocking in at 29.94 seconds.
🇸🇪 The Swedish brand shrugged and sent the Regera back onto the track. Guess what? 28.81 seconds. How do you like that, Mate Rimac?
🥇 The margin of victory was 1.13 seconds, which in such a discipline, it must be said, is quite impressive. Surely, Rimac will have to respond. Competing is possible, but it will definitely require some effort.
🏎️Interestingly enough, the Regera has already been discontinued, with the last unit delivered to its owner in 2022. So why set a record with a car that is no longer in production? Koenigsegg answers bluntly: "Because it can!" The Regera remains superior to any newcomer. And it has proven it once again.
💥To outperform a significant rival - an electric car that has just set 23 records - is both impressive and beautiful. 💥
#Koenigsegg#Regera#driving#car