![](/img/trans.png)
[英]python 3 : how to wait for the callback in async function when use asyncio tasks
[英]Python: use asyncio module to wait result for 2 independent tasks
如果我得到你想要做的...
为了同时运行这两个函数,您可以使用asyncio.create_task和asyncio.wait来获取它们的结果。
看看下面的例子:
import sys
import asyncio
async def test_1():
return sys._getframe().f_code.co_name
async def test_2():
return sys._getframe().f_code.co_name
async def main():
t1 = asyncio.create_task(test_1())
t2 = asyncio.create_task(test_2())
done, pending = await asyncio.wait([t1, t2])
results = [d.result() for d in done]
# The order of the finished function is unknown
print(results)
if __name__ == '__main__':
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
loop.close()
它将运行两个函数并等待它们完成。
一旦他们done
它会聚集在两者的结果done
功能的results
列表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.