简体   繁体   中英

Non-blocking requests in Sanic framework

I'm trying out Sanic and ran the Hello World app except I added a sleep in the request handler:

@app.route("/")
async def test(request):
    time.sleep(5)
    return json({"hello": "world"})

However, when I run this, it still blocks on each request:

$ 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]

In two separate terminals:

$ 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

I thought the idea of Sanic is being able to process all requests asynchronously and not blocking until one completes to process the next one. Am I missing something here?

time.sleep(5)替换为:

 await asyncio.sleep(5)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM