发布于2024-12-12 11:01 阅读(985) 评论(0) 点赞(19) 收藏(1)
I have four total coroutines (async def
functions): A, B, C, and D. C can't be called until I know the result of A, and D can't be called until I know the result of A and B. See the attached diagram. I want to run these coroutines concurrently such that the total time spent until I get the results from C and D is minimized.
We should start by running A and B concurrently, but I it would be sub-optimal to use a, b = asyncio.gather(get_a(), get_b())
because C should be started as soon as A finishes, and doesn't need to wait around for B to complete.
Similarly, we could run A and C in sequence, while also running B concurrently (as shown below) but in this case we might be waiting around to start D longer than needed if A and B have completed but C is still running:
async def group_1():
a = await get_a()
c = await get_c(a)
return c
c, b = asyncio.gather(
group_1(),
get_b(),
)
How can I execute these tasks without any unnecessary waiting?
I imagine I could use callbacks if necessary, but I would prefer to use a higher level construct if possible. Additionally, my actual use case has more tasks than this with more complex dependency chains, and I think callbacks would quickly become difficult to reason about.
OP here. I think I've come up with a solution for this problem, which is to use asyncio.create_task()
to start A and B in the background. C and D can then both await
the A
task (full example below). I'm still not certain if this is the cleanest way to do it though, so additional answers are still welcome.
import asyncio
async def get_a():
await asyncio.sleep(1)
return "a"
async def get_b():
await asyncio.sleep(1)
return "b"
async def get_c(a):
await asyncio.sleep(1)
return "c"
async def get_d(a, b):
await asyncio.sleep(1)
return "d"
async def main():
a_task = asyncio.create_task(get_a())
b_task = asyncio.create_task(get_b())
async def c_wrapper():
a = await a_task
return await get_c(a)
async def d_wrapper():
a = await a_task
b = await b_task
return await get_d(a, b)
return await asyncio.gather(c_wrapper(), d_wrapper())
asyncio.run(main())
作者:黑洞官方问答小能手
链接:https://www.pythonheidong.com/blog/article/2046454/cea8b280300f2a36fbc1/
来源:python黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 python黑洞网 All Rights Reserved 版权所有,并保留所有权利。 京ICP备18063182号-1
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!