Unbeknownst to everyone, on May 12, 2021, a new version of the well-known Flask microframework was released. Although it seemed that Flask already has everything, well, almost everything that is needed for a microframework.
Anticipating interest, and what is new, I will leave a link to the Change log .
From the vending features of the new version:
Dropped support for Python version 2. Minimum Python 3.6
Support for asynchronous views and other callbacks such as error handlers defined with async def. Regular synchronous views continue to work unchanged. ASGI features such as websockets are not supported.
Add route decorators for common HTTP API methods.
@ app.post ("/ login") == @ app.route ("/ login", methods = ["POST"])
New function Config.from_file to load configuration from any file format.
The flask shell command enables tab completion, just like a regular python shell does.
When serving static files, browsers will cache based on content rather than based on a 12 hour timer. This means changes to static content such as CSS styles will be reflected immediately on reload without having to clear the cache.
Consider asynchrony
Everything would be fine, but at the very beginning after installation the asgiref module was not found. We will install it by hand.
For example, let's write the simplest application: Ping / Pong. It does not have much sense and complex logic, it only imitates some check if the service is alive. Also, this application will become a benchmark.
from flask import Flask
app = Flask(__name__)
@app.get('/')
async def ping():
return {'message': 'pong'}
if __name__ == '__main__':
app.run(host='0.0.0.0')
Deploy
As the Change log said: "ASGI functions such as websockets are not supported."
That is, the only way to deploy an application is using gunicorn.
: gunicorn -w 8 --bind 0.0.0.0:5000 app:app
-w 8 - 8
--bind 0.0.0.0:5000 -
: wrk -t 8 -c 100 -d 5 http://localhost:5000
Flask 2.0:
Running 5s test @ http://localhost:5000
8 threads and 100 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 17.80ms 2.37ms 36.95ms 91.92%
Req/Sec 673.44 163.80 3.86k 99.75%
26891 requests in 5.10s, 4.21MB read
Requests/sec: 5273.84
Transfer/sec: 844.69KB
Flask 2.0:
Running 5s test @ http://localhost:5000
8 threads and 100 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 4.91ms 842.62us 21.56ms 89.86%
Req/Sec 2.38k 410.20 7.64k 93.53%
95301 requests in 5.10s, 14.91MB read
Requests/sec: 18689.25
Transfer/sec: 2.92MB
Flask 1.1.2:
Running 5s test @ http://localhost:5000
8 threads and 100 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 4.98ms 823.42us 17.40ms 88.55%
Req/Sec 2.37k 505.28 12.23k 98.50%
94522 requests in 5.10s, 14.78MB read
Requests/sec: 18537.84
Transfer/sec: 2.90MB
, 1 2 ( ). Flask 2.0 , dev view . ASGI , uvicorn. .
major , . , . Flask, , Django.
If you have any ideas why these results were obtained, please share them in the comments.