Gevent is the use of simple, sequential programming in python to achieve scalability provided by asynchronous IO and lightweight multi-threading (as opposed to the callback-style of programming using Twisted’s Deferred).
It is built on top of libevent/libev (for asynchronous I/O) and greenlets (lightweight cooperative multi-threading).
we can use pip install gevent to install it.
Here is the sample.py:
from gevent import monkey monkey.patch_all() from flask import Flask from gevent import pywsgi app = Flask(__name__) @app.route('/') def hello(): return "Hello World!" if __name__ == '__main__': #app.run() server = pywsgi.WSGIServer(("0.0.0.0", 8080), app) server.serve_forever()
you can run it in background:
nohup python3 sample.py &
then you can open the sample app on http://your_server_ip:8080
Use ApacheBench To Do Load Testing:
1. apt-get install apache2-utils
2. ab -r -n 2000 -c 200 http://127.0.0.1:8080/?delay=1
Concurrency Level: 200 Time taken for tests: 2.470 seconds Complete requests: 2000 Failed requests: 0 Total transferred: 294000 bytes HTML transferred: 24000 bytes Requests per second: 809.60 [#/sec] (mean) Time per request: 247.035 [ms] (mean) Time per request: 1.235 [ms] (mean, across all concurrent requests) Transfer rate: 116.22 [Kbytes/sec] received
compare it with sample_old.py without gevent:
from flask import Flask app = Flask(__name__) @app.route('/') def hello(): return "Hello World!" if __name__ == '__main__': app.run(port=8080)
run same ab command, you will get:
Concurrency Level: 200 Time taken for tests: 4.493 seconds Complete requests: 2000 Failed requests: 0 Total transferred: 330000 bytes HTML transferred: 24000 bytes Requests per second: 445.10 [#/sec] (mean) Time per request: 449.340 [ms] (mean) Time per request: 2.247 [ms] (mean, across all concurrent requests) Transfer rate: 71.72 [Kbytes/sec] received
Time per request reduced from 449ms to 247ms!
ref: https://iximiuz.com/en/posts/flask-gevent-tutorial/