Skip to content

Sanic #
Find similar titles

Structured data

Category
Programming
URL

Sanic #

Overview #

Sanic is a Python 3.5 web server which is so fast. Sanic is asynchronous request handler. Meaning, we can use new async/await syntax from Python 3.5 with making our code non-blocking and very fast.

Installation #

Sanic like other Python packages can be installed via pip.

$ pip install sanic

Usage example #

To figure out how to get started quickly we can write very simple example.

from sanic import Sanic
from sanic.response import json

app = Sanic()

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

if __name__ == "__main__":
    app.run(host="127.0.0.1", port=8000)

Features #

Sanic offers these basic features like other frameworks.

  1. Request handling
  2. Routing
  3. Middleware
  4. Exceptions
  5. Blueprints
  6. Class based views
  7. Cookies
  8. Static file serving

Benchmarks #

Benchmarks have done with other Python web servers.

Server Implementation Requests/sec Avg. Latency
Sanic Python 3.5 + uvloop 33,342 2.96 ms
Wheezy Gunicorn + Meinheld 20,244 4.97 ms
Falcon Gunicorn + Meinheld 18,972 5.27 ms
Bottle Gunicorn + Meinheld 13,596 7.36 ms
Flask Gunicorn + Meinheld 4,988 20.08 ms
Kyoukai Python 3.5 + uvloop 3,889 27.44 ms
Aiohttp Python 3.5 + uvloop 2,979 33.42 ms
Tornado Python 3.5 2,138 46.66 ms

References #

  1. https://github.com/channelcat/sanic
  2. https://magic.io/blog/uvloop-blazing-fast-python-networking/
  3. https://pypi.python.org/pypi/Sanic/0.1.9
0.0.1_20210630_7_v33