我学习和探索 Python asyncio
有一段时间了。在开始这段旅程之前,我阅读了大量文章以了解多线程
、multiprocessing
和asyncio
之间的细微差别。但是,据我所知,我遗漏了一个基本问题。我将尝试在下面解释伪代码的含义。
import asyncio
import time
async def io_bound():
print("Running io_bound...")
await asyncio.sleep(3)
async def main():
start = time.perf_counter()
result_1 = await io_bound()
result_2 = await io_bound()
end = time.perf_counter()
print(f"Finished in {round(end - start, 0)} second(s).")
asyncio.run(main())
当然,这将需要大约 6 秒,因为我们直接调用了 io_bound
协程两次,并没有将它们放入事件循环中。这也意味着它们不是同时运行的。如果我想同时运行它们,我将不得不使用 asyncio.gather(*tasks)
特性。我同时运行它们肯定只需要 3 秒。
让我们想象这个 io_bound
协程是一个查询数据库以取回一些数据的协程。该应用程序可以大致如下使用 FastAPI 构建。
from fastapi import FastAPI
app = FastAPI()
@app.get("/async-example")
async def async_example():
result_1 = await get_user()
result_2 = await get_countries()
if result_1:
return {"result": result_2}
return {"result": None}
假设 get_user
和 get_countries
方法各需要 3 秒,并且正确实现了异步查询。我的问题是:
- 我需要为这两个数据库查询使用
asyncio.gather(*tasks)
吗?如果需要,为什么?如果不是,为什么? - 我调用两次的
io_bound
与我连续调用的get_user
和get_countries
有什么区别?上面的例子? - 在
io_bound
示例中,如果我在FastAPI 中做同样的事情,是否只需要6 秒就可以返回响应?如果是这样,为什么不是 3 秒? - 在 FastAPI 的上下文中,什么时候是在端点中使用
asyncio.gather(*tasks)
的合适时机?
最佳答案
- 我需要为这两个数据库使用asyncio.gather(*tasks)吗 查询?如果需要,为什么?如果不是,为什么?
你需要吗?不,你所做的工作。该请求将花费 6 秒但不会阻塞,因此如果您有另一个请求进入,FastAPI 可以同时处理这两个请求。 IE。同时进入的两个请求仍然需要 6 秒,而不是 12 秒。
如果 get_user() 和 get_countries() 这两个函数相互独立,那么您可以使用 asyncio.gather
或许多其他方法中的任何一种来同时运行这两个函数在 asyncio 中,这意味着请求现在只需 3 秒。例如:
async def main():
start = time.perf_counter()
result_1_task = asyncio.create_task(io_bound())
result_2_task = asyncio.create_task(io_bound())
result_1 = await result_1_task
result_2 = await result_2_task
end = time.perf_counter()
print(f"Finished in {round(end - start, 0)} second(s).")
或
async def main_2():
start = time.perf_counter()
results = await asyncio.gather(io_bound(), io_bound())
end = time.perf_counter()
print(f"Finished in {round(end - start, 0)} second(s).")
- 我调用了两次的 io_bound 和 get_user 和 get_countries,我在上面调用回来 例子?
假设 get_user 和 get_countries 只是调用 io_bound,什么都没有。
- 在 io_bound 示例中,如果我在 FastAPI 中执行相同的操作, 难道只需要6秒就可以回复吗?如果是这样,为什么 不是 3 秒吗?
这将需要 6 秒。 FastAPI 不会变魔术来改变函数的工作方式,它只是允许您创建一个可以轻松运行异步函数的服务器。
- 在 FastAPI 的上下文中,什么时候使用合适 端点中的 asyncio.gather(*tasks)?
当你想同时运行两个或多个异步函数时。这一点是相同的,无论您使用的是 FastAPI 还是 python 中的任何其他异步代码。
关于python - 依次等待多个异步函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73843521/