Три способа выполнить множество задач с 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
📢TODAY!
What happens when the #antimicrobial drugs used in treating infections in humans & animals become ineffective?
At 6 PM, join the DRASA - Dr. Ameyo Stella Adadevoh Health Trust 's conversation on "You, Me, and The Supergerms: Preventing Antimicrobial Resistance Together"
🔗Share
https://bit.ly/DRASAWAAW22
ICYMI
"#AntimicrobialResistance requires the efforts of the Human, Animal & Environmental sectors & also a whole-of-society approach to manage"
Watch our #AMR programme coordinator, Dr Tochi Okwor as she discusses the impact of #antimicrobial misuse in Nigeria.
🔗
https://youtu.be/B1GgpIuoE2Y
#Antimicrobial Resistance (AMR) is a global threat.
✨As a healthcare professional, always carry out the correct test before prescribing.
Accurate diagnosis helps prevent resistance☑️
Always test before treating!!
#StopAMR#StaySafe#NCDCNigeria
#Antimicrobial Resistance (AMR) is a growing global health threat — but together, we can prevent it! 🚫💊
👉 Don’t self-medicate
👉 Complete your prescriptions
👉 Don’t share leftover drugs
👉 wash hands frequently with soap under running water
👉 Stay protected with vaccines
Let’s all take responsibility in the fight against AMR. 💪🌍
#StopAMR#StaySafe#NCDCNigeria
As an individual, what can you do to prevent #Antimicrobial Resistance?
Our #AMR programme manager @beeodune will be live alongside other panellists on @drasatrust's @XSpaces discussion to speak on:
“Supergerms and the Future of Infection :
🗓️ Today, 24th November
🕙6:00 PM
🔗Join the conversation via:
http://bit.ly/LearnWithDRASA7
#WAAW2023#WorldAMRAwarenessWeek