Why Sanic Beats Flask: Speed Benchmarks, Setup, and Advanced Features
This article compares Sanic's performance with other Python web frameworks, shows how to install and run a Sanic app, demonstrates request handling, routing, middleware, exception handling, and blueprint usage, and explains why Sanic’s modern async features make it a fast, popular choice.
Speed Comparison
Sanic Python 3.5 + uvloop 30,601 req/s 3.23 ms
Wheezy gunicorn + meinheld 20,244 req/s 4.94 ms
Falcon gunicorn + meinheld 18,972 req/s 5.27 ms
Bottle gunicorn + meinheld 13,596 req/s 7.36 ms
Flask gunicorn + meinheld 4,988 req/s 20.08 ms
Kyoukai Python 3.5 + uvloop 3,889 req/s 27.44 ms
Aiohttp Python 3.5 + uvloop 2,979 req/s 33.42 msInstallation
python -m pip install sanicHello World
from sanic import Sanic
from sanic.response import json
app = Sanic(__name__)
@app.route("/")
async def test(request):
return json({"hello": "world"})
app.run(host="0.0.0.0", port=8000)Running
python3 main.pyRequest Object
Attributes
request.files – dictionary of uploaded File objects
request.json – JSON payload
request.args – query string parameters
request.form – POST form data
Request Handling Examples
from sanic import Sanic
from sanic.response import json
@app.route("/json")
async def post_json(request):
return json({"received": True, "message": request.json})
@app.route("/form")
async def post_form(request):
return json({"received": True, "form_data": request.form, "test": request.form.get('test')})
@app.route("/files")
async def upload_file(request):
test_file = request.files.get('test')
file_parameters = {
'body': test_file.body,
'name': test_file.name,
'type': test_file.type,
}
return json({"received": True, "file_names": list(request.files.keys()), "test_file_parameters": file_parameters})
@app.route("/query_string")
async def query_string(request):
return json({
"parsed": True,
"args": request.args,
"url": request.url,
"query_string": request.query_string
})Routing
from sanic import Sanic
from sanic.response import text
@app.route('/tag/<tag>/')
async def tag_handler(request, tag):
return text(f'Tag - {tag}')
@app.route('/number/<integer_arg>/')
async def int_handler(request, integer_arg):
return text(f'Integer - {integer_arg}')
@app.route('/person/<name>/')
async def person_handler(request, name):
return text(f'Person - {name}')Middleware Registration
app = Sanic(__name__)
@app.middleware('request')
async def request_middleware(request):
print("I am a spy")
# optionally return a response to halt the request
# return text('I halted the request')
@app.middleware('response')
async def response_middleware(request, response):
# modify response if needed
return responseException Handling
from sanic import Sanic
from sanic.exceptions import ServerError, NotFound
from sanic.response import text
@app.route('/killme')
def i_am_ready_to_die(request):
raise ServerError("Something bad happened")
@app.exception(NotFound)
def ignore_404s(request, exception):
return text(f"Yep, I totally found the page: {request.url}")Blueprints
from sanic import Blueprint, Sanic
from sanic.response import json
bp = Blueprint('my_blueprint')
@bp.route('/')
async def bp_root():
return json({'my': 'blueprint'})
app = Sanic(__name__)
app.register_blueprint(bp)
app.run(host='0.0.0.0', port=8000, debug=True)Conclusion
Sanic is expected to become a popular framework because it leverages Python 3.5+ features that make applications run faster.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
