Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 860
mart1n Avatar asked Feb 18 '17 18:02

mart1n


1 Answers

Replace time.sleep(5) with:

 await asyncio.sleep(5)
like image 174
Udi Avatar answered Sep 27 '22 15:09

Udi