Три способа выполнить множество задач с 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
#Idiom
💢Go off on a tangent
Or
💢 Go off at a tangent
🔰If a person or piece of writing goes off on a tangent or goes off at a tangent, they start saying or thinking something that is not directly connected with what they were saying or thinking before
💠Note:-
In geometry, a tangent is a straight line which touches a curve at one point
🕸 Ex:- Our teacher would occasionally go off on a tangent and start talking about something totally unrelated to the textbook.
💠Note:-
You can use other verbs instead of go
🕸 Ex:- The book's theme wandersoff on a tangent now and then.
#Idiom
🕸People in society🕸
❄️A self-made man
♣️ A person who is rich and successful because they have worked hard, not because they were born into a rich family
❄️The chattering classes
♣️ Educated people who enjoy discussing social, political and cultural issues
❄️A second-class citizen
♣️ Someone is treated as if they are less important than others in society
❄️The grass roots
♣️ Ordinary people in a political or sporting organisation, not the leaders
❄️ The silent majority
♣️ A large number of people who do not express their opinions publicly
❄️ The (men in) grey suits
♣️ People in business or politics with a lot of influence or power, although they are not well known to the public
❄️Public enemy number one
♣️ Smth or someone that a lot of people dislike or disapprove of
❄️New kid on the block
♣️ Someone who is new in a place or organisation and has many things to learn about it (informal).
#Idiom
💢Even exchange: a trade of equal value; when you return something and take something else that costs the same price
✨ Ex: If you return that jacket and get this sweater instead, it will be an even exchange. They're both $39.99.
#Idiom
💢(To) steer clear of: to avoid, usually due to a bad experience
✨ Ex: Mark got food poisoning at O'Reilly's? We'd better steer clear of that place from now on!
#Idiom
💢(To) have the magic touch: to have talent at doing something; to be able to do something difficult.
✨ Ex: You can't open that bottle? Give it to Ivan. He usually has the magic touch.