Sanic 框架中的非阻塞请求
Non-blocking requests in Sanic framework
我正在尝试 Sanic 和 运行 Hello World 应用程序,除了我在请求处理程序中添加了睡眠:
@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)
我正在尝试 Sanic 和 运行 Hello World 应用程序,除了我在请求处理程序中添加了睡眠:
@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)