Gunicorn
gunicorn is a python-based Web Servers Gateway Interface for unix; it is a bridge between the web server receiving requests and the python app application;
if you don't use gunicorn, just use Flask, for a large number of simultaneous requests, it will crash and errors will occur; And gunicorn is for a large number of highly concurrent requests, can realize the request to reach the server, and make a response, and can increase/decrease the worker in time according to the load demand, so as to achieve the role of management coordination.
basic introduction
Gunicorn is a way to start the app program, which is different from starting the app program directly in the main file, which is more convenient and manageable;
three startup methods:
-
workers mode
Each worker is a UNIX process that loads the python application No shared memory between workers
The recommended number of workers is
(2*CPU) + 1
; -
multi-threaded mode
Gunicorn also allows each worker to have multiple threads. In this mode, each worker will be loaded once, and each thread generated by the same worker will share the same memory space. Use threads mode, use threads mode every time, worker class It will be gthread;
gunicorn -w 5 --threads=2 main:app #等同于 gunicorn -w 5 --thread=2 --worker-class=gthread main:app
-
Pseudo-thread gevent (coroutine)
gunicorn --worker-class=gevent --worker-connections=1000 -w 3 main:app
work-connections is a special setting for the gevent worker class; the recommended number of workers is still
(2*CPU) + 1
;In this case, the maximum number of concurrent requests is 3000 (3 workers * 1000 connections/worker).
basic use
Start with a configuration file
gunicorn --config=config.py myapp.test:app
#myapp.test为文件,第二个指的是flask应用的名字;
close gunicorn
pstree -ap|grep gunicorn #寻找masterpid
kill -HUP 9479 #重启Gunicorn任务
kill -9 9479 #退出gunicorn任务
gunicorn address used
If you use gunicorn to start, you don't need tomian
start it, and you don't need to use itapp.run()
;
#gunicorn 启动
if __name__ != '__main__':
#日志的绑定
gunicorn_logger = logging.getLogger(‘gunicorn.error’)#获得gunicorn的logger
#将gunicorn的logger和flask app的logger绑定在一起
app.logger.handlers = gunicorn_logger.handlers
#将绑定的logger 的level设置成gunicorn logger的level, 因为最终输出的log level是在gunicorn中配置的
app.logger.setLevel(gunicorn_logger.level)
#启动分析任务
crossline_t = threading.Thread(target=crossline_analyze.task_start)
crossline_t.start()
- Exit gunicorn
to end the process according to the pid number in the pid filekill -QUIT pid kill -TERM pid
ref:
gunicorn configuration file
gunicorn documentation
Gunicorn introduction and pressure measurement
gunicorn log binding
reference 01
The role of the pid file
common mistakes
-
No module named <module_name>" while running gunicorn
Celery cannot be found in gunicorn. By checking, wewhich gunicorn
can see that the default call is gunicorn under local; uninstall and reinstall gunicorn in the virtual environment where celery is installed;
when using it, you need to specify the specific location of gunicorn in the virtual environment;
ifbad interpreter
an error occurs , needs to be in progressbin/gunicorn
, edit the python interpreter path in the first line;
soft link the gunicorn in the virtual environment to the local path, then use without specifying the specific path; -
gunicorn.errors.HaltServer
--preload
Add to display specific errors at startup ;