【发布时间】:2021-07-05 23:39:57
【问题描述】:
我是 asyncio 的新手。看了asyncio documentation,不明白Tasks在哪里运行。
import asyncio
async def query_api(url):
#http://a.b.c takes 3 seconds
#http://x.y.z takes 5 seconds
async def main():
task1 = asyncio.create_task(query_api('http://a.b.c'))
task2 = asyncio.create_task(query_api('http://x.y.z'))
await task1
await task2
print ('Tasks Done')
asyncio.run(main())
This doc 表示 asyncio 有一个在主线程中运行的事件循环。
-
task1和task2是否在主线程中运行?如果是这样,它们一定不能并行运行,因为 CPU 一次只能做一件事? -
Tasks Done会在 8 秒或更早后出现?
【问题讨论】:
标签: python python-3.x python-asyncio