Sanic: Why here didn't use ```async def```âť“

Created on 24 Dec 2016  Â·  4Comments  Â·  Source: sanic-org/sanic

Most helpful comment

@neo1218 and @luizfb I did test using WRK and I've a few better performance with the async keyword.

Machine:
OS: Fedora25
Intel® Core™ i3 CPU M 370 @ 2.40GHz × 4
3,7 GiB

without async keyword:

âžś  ~ wrk -c 100 -d 30  http://0.0.0.0:8000
Running 30s test @ http://0.0.0.0:8000
  2 threads and 100 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency     5.35ms    2.07ms  30.65ms   76.26%
    Req/Sec     9.43k   656.42    10.64k    72.67%
  563060 requests in 30.04s, 73.03MB read
Requests/sec:  18740.64
Transfer/sec:      2.43MB

With async keyword:

âžś  ~ wrk -c 100 -d 30  http://0.0.0.0:8000
Running 30s test @ http://0.0.0.0:8000
  2 threads and 100 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency     5.05ms    2.12ms  57.27ms   79.70%
    Req/Sec    10.02k   573.57    11.08k    57.50%
  598465 requests in 30.03s, 77.62MB read
Requests/sec:  19931.79
Transfer/sec:      2.59MB

All 4 comments

Because it makes no difference regarding what the examples are trying to explain.

The async directive is completely optional, you can add it whenever you want that function to be treated asynchronously or not. It changes nothing on how the request data parameter is treated.

@luizfb Thanks for your answer🙏
but if don't add async, will affect speed?

@neo1218 and @luizfb I did test using WRK and I've a few better performance with the async keyword.

Machine:
OS: Fedora25
Intel® Core™ i3 CPU M 370 @ 2.40GHz × 4
3,7 GiB

without async keyword:

âžś  ~ wrk -c 100 -d 30  http://0.0.0.0:8000
Running 30s test @ http://0.0.0.0:8000
  2 threads and 100 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency     5.35ms    2.07ms  30.65ms   76.26%
    Req/Sec     9.43k   656.42    10.64k    72.67%
  563060 requests in 30.04s, 73.03MB read
Requests/sec:  18740.64
Transfer/sec:      2.43MB

With async keyword:

âžś  ~ wrk -c 100 -d 30  http://0.0.0.0:8000
Running 30s test @ http://0.0.0.0:8000
  2 threads and 100 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency     5.05ms    2.12ms  57.27ms   79.70%
    Req/Sec    10.02k   573.57    11.08k    57.50%
  598465 requests in 30.03s, 77.62MB read
Requests/sec:  19931.79
Transfer/sec:      2.59MB

@neo1218 The async keyword is what makes Sanic treat your requests asynchronously, which is where most of the performance benefits from Sanic come from, hence there will logically be a performance difference from using async or not, as evidenced by @viniciusfeitosa 's benchmarks.

You probably want to add async to all your route handlers (the functions you decorate with @app.route) for the performance benefits. If you call those functions from other parts of your code as well, you might want to read up on Python 3's new async/await functionalities and learn where you might need to use an await to maintain synchronicity.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Souldat picture Souldat  Â·  3Comments

rainyear picture rainyear  Â·  3Comments

woutor picture woutor  Â·  3Comments

geekpy picture geekpy  Â·  4Comments

sirex picture sirex  Â·  4Comments