FastAPI行为不异步

弗洛伦丁·亨内克(Florentin Hennecker)

我可能没有正确理解FastAPI中的异步概念。

我正在同时从两个客户端访问以下应用程序的根端点。我希望FastAPI打印Started两次:

from fastapi import FastAPI
import asyncio

app = FastAPI()

@app.get("/")
async def read_root():
    print('Started')
    await asyncio.sleep(5)
    print('Finished')
    return {"Hello": "World"}

相反,我得到以下内容,它们看起来非常不异步:

Started
Finished
INFO: ('127.0.0.1', 49655) - "GET / HTTP/1.1" 200
Started
Finished
INFO: ('127.0.0.1', 49655) - "GET / HTTP/1.1" 200

我想念什么?

PawełŻukowski

您如何确保同时有多个请求?

您的代码很好-尝试使用以下命令进行测试:

for n in {1..5}; do curl http://localhost:8000/ & ; done

您的浏览器可能正在将后续请求缓存到同一URL。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章