8

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?

mart1n
  • 5,969
  • 5
  • 46
  • 83

1 Answers1

15

Replace time.sleep(5) with:

 await asyncio.sleep(5)
Udi
  • 29,222
  • 9
  • 96
  • 129