Три способа выполнить множество задач с 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
😄Accept
➖➖➖➖➖➖
Accept is a regular verb. Here are five common ways to use it:
🔘to agree to take something or receive something willingly, for example a gift, an award, an apology, responsibility for something or even a form of payment.
🔜Many shops don’t accept cash anymore.
🔜The actor accepted a life achievement award at a special ceremony.
🔘 to say yes to an invitation or an offer.
🔜She accepted the job straight away!
🔜I accepted Leon's dinner invitation from us both. I hope that's OK!
🔘to believe or recognise that something is true.
🔜The relationship has been over for weeks, but he still refuses to accept it.
🔜The police officer didn't accept the suspect's answer.
🔘to consider satisfactory or to give approval.
🔜Tommy was immediately accepted by the other children at his new school.
🔜Her boyfriend's family welcomed and accepted her straight away.
🔘to endure something without complaint.
🔜He accepted that he had lost the match.
🔜Even though it makes me sad, I accept that my one minute with you is up.
#Accept👨🏫@America
➖➖➖➖➖➖➖➖➖➖➖➖
🆕 Crypto News @Money
😁 Crypto Game @Egame
🇺🇸 US News @America
🇯🇵 Japan News @Japan
🇦🇪 UAE News @Dubai
▶️ Popular Movies @Videos
😜 Best Funny Video @Funnys
😄Accept
➖➖➖➖➖➖
Accept is a regular verb. Here are five common ways to use it:
🔘to agree to take something or receive something willingly, for example a gift, an award, an apology, responsibility for something or even a form of payment.
🔜Many shops don’t accept cash anymore.
🔜The actor accepted a life achievement award at a special ceremony.
🔘 to say yes to an invitation or an offer.
🔜She accepted the job straight away!
🔜I accepted Leon's dinner invitation from us both. I hope that's OK!
🔘to believe or recognise that something is true.
🔜The relationship has been over for weeks, but he still refuses to accept it.
🔜The police officer didn't accept the suspect's answer.
🔘to consider satisfactory or to give approval.
🔜Tommy was immediately accepted by the other children at his new school.
🔜Her boyfriend's family welcomed and accepted her straight away.
🔘to endure something without complaint.
🔜He accepted that he had lost the match.
🔜Even though it makes me sad, I accept that my one minute with you is up.
#Accept👨🏫@America
➖➖➖➖➖➖➖➖➖➖➖➖
🆕 Crypto News @Money
😁 Crypto Game @Egame
🇺🇸 US News @America
🇯🇵 Japan News @Japan
🇦🇪 UAE News @Dubai
▶️ Popular Movies @Videos
😜 Best Funny Video @Funnys