Три способа выполнить множество задач с 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
#selfimprovement
Emotional Intelligence: Why It Matters More Than IQ🧠
You’re not as rational as you think you are. None of us are. Neuroscience shows that our amygdala, the brain’s emotional center, often fires before the prefrontal cortex—our logical HQ—has even had time to weigh in.
Finally reached 300 members
Thank you so much for all your wonderful support 😊😍
#300Members#ipersonalgrowths
#selfimprovement#Mentorship
PS : Once Again Thanks all dear Amazing people 🌻🌿 Let's go ahead 😀 fulfill You're Dream and Ambitious
Stay Connect 🌼💫 Keep Supporting 💛
As you may have noticed one month into the new year, traditional New Year’s resolutions often fail because people set vague or too big goals, rely only on willpower, and don’t have a clear plan. Small, specific changes and building habits step by step are supposed to work better. 🔄
[Read more]
@googlefactss
#NewYearsResolutions#Habits#Goals#SelfImprovement
The Power of Your Thoughts - #stoicismquotes#powerofthought#selfimprovement#motivationalspeech
https://t.me/LanguageStuff
Learning should be easy and enjoyable 😎👍
Rich Dad vs Poor Dad mindset 💸
Poor Dad: “Money comes when you work.”
Rich Dad: “Money comes when you build a system.”
The lesson:
Don’t only trade time for money.
Build assets, skills, and processes that pay you repeatedly.
Today’s action:
Pick ONE system to start:
• automate savings/investing
• create a digital product
• build a side business workflow
#RichDadPoorDad#money#wealth#mindset#financialfreedom#selfimprovement#books
How to Get Ahead of 99% of People (Starting Today)
2023-03-20 by Mark Manson
#markmanson#markmanson#selfimprovement#howtochangeyourlife#betterideas#selfimprovementtips#selfimprovementbooks
#YouTube#liked