Три способа выполнить множество задач с 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
#rstats#book
📕
Introducción a R: aprendiendo R sin morir en el intento.
Libro, en formato web y en español, ideal para aquellos interesados en iniciar el aprendizaje de #R a partir de los tipos de datos y la programación.
🌐ES
🔗Link
-----
Canal principal: @repo_science
Cupones: @freecoupons_reposcience
-----
#NYR2024#RStats
🖥
Excited to have Hadley Wickham, Chief Scientist at Posit, speaking at NY R Conference 2024! Dive into the evolution of data science and discover future trends with one of the pioneers in R programming.
rstats.ai/nyr.html
-----
Main channel: @repo_science
Coupons: @freecoupons_reposcience
-----
#DataScience#rstats#DataScientist#dataanalysis#r#programming#dataviz#statistics
In an era driven by data, harnessing the power of statistics is key to deriving meaningful insights from the vast sea of information around us.
pyoflife.com/statistics-wit
-----
Main channel: @repo_science
Coupons: @freecoupons_reposcience
-----
#ML#datascience#rstats#programming#dataviz#stats#analytics#machinelearning
6 formas de visualizar los resultados del análisis cluster jerárquico con R
-----
Main channel: @repo_science
Coupons: @freecoupons_reposcience
-----
#programming#GPT4#GPT3#dataviz#stats#analytics#ML#AI#IA
🚀
Cómo programar más rápidamente en #RStats con #ChatGPT
Escribir código es un proceso lento, especialmente cuando estás aprendiendo #datascience por primera vez. ¿Y si pudieras acelerarlo?
🔗Link
-----
Main channel: @repo_science
Coupons: @freecoupons_reposcience
-----