Why Sanic Outperforms Flask: Speed Benchmarks and Quick‑Start Guide
This article compares Sanic's performance with other Python web frameworks, provides installation steps, a Hello World example, request handling, routing, middleware, exception handling, and blueprint usage, demonstrating why Sanic is a high‑performance alternative to Flask.
Speed Comparison
Benchmarks show Sanic (Python 3.5 + uvloop) handling 30,601 requests per second at 3.23 ms, outperforming Wheezy (20,244 req/s, 4.94 ms), Falcon (18,972 req/s, 5.27 ms), Bottle (13,596 req/s, 7.36 ms), Flask (4,988 req/s, 20.08 ms), Kyoukai (3,889 req/s, 27.44 ms) and Aiohttp (2,979 req/s, 33.42 ms).
Installation
Requires Python 3.5+. Install with:
python -m pip install sanicHello World
Create main.py with:
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)Run with python3 main.py.
Request Object
Key attributes: request.files (uploaded files), request.json, request.args, request.form.
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(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
Similar to Flask, routes can capture variables:
@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}')Middleware Registration
@app.middleware('request')
async def request_middleware(request):
print("I am a spy")
@app.middleware('response')
async def response_middleware(request, response):
return text('I halted the response')Exception Handling
from sanic.exceptions import ServerError
@app.route('/killme')
def kill(request):
raise ServerError("Something bad happened")Blueprints
Blueprints organize large projects, similar to Flask blueprints:
from sanic import Blueprint
from sanic.response import json
bp = Blueprint('my_blueprint')
@bp.route('/')
async def bp_root():
return json({'my': 'blueprint'})Register with app.register_blueprint(bp) and run the app.
Conclusion
Sanic leverages Python 3.5+ async features and uvloop to deliver high‑performance web services, making it a strong alternative to Flask.
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.
