我正在尝试 Sanic并运行 Hello World 应用程序,除了我在请求处理程序中添加了一个 sleep :
@app.route("/")
async def test(request):
time.sleep(5)
return json({"hello": "world"})
但是,当我运行它时,它仍然会阻塞每个请求:
$ python app.py
2017-02-18 19:15:22,242: INFO: Goin' Fast @ http://0.0.0.0:8000
2017-02-18 19:15:22,245: INFO: Starting worker [15867]
在两个独立的终端中:
$ time curl http://0.0.0.0:8000/
{"hello":"world"}
real 0m5.009s
user 0m0.003s
sys 0m0.001s
$ time curl http://0.0.0.0:8000/
{"hello":"world"}
real 0m9.459s
user 0m0.000s
sys 0m0.004s
我认为 Sanic 的想法是能够异步处理所有请求,并且在一个完成处理下一个请求之前不会阻塞。我在这里遗漏了什么吗?
最佳答案
将 time.sleep(5)
替换为:
await asyncio.sleep(5)
关于python - Sanic 框架中的非阻塞请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42319075/