Python's common Web deployment is paired with Nginx+gunicorn, which records the use of Gunicorn configuration.
Installing Gunicorn
pip install gunicorngunicorn -h # 查看使用的命令
Gunicorn launching a flask application
# app.pyfromimport Flask def create_app(): = Flask(__name__) return app = create_app() @app.route(‘/‘) def index(): return‘hello world!‘if__name__==‘__main__‘: app.run()
- Starting in the directory of the Flask project
# 第一个app指的是app.py文件,第二个指的是flask应用的名字;gunicorn -w 4 -b 0.0.0.0:8000 app:app
Gunicorn's parameters
-C Config:config, the path of the configuration file, start with the configuration file, production environment use,-B address:address,ip Port, bound run host,-W int,--workers int: The number to process the worker process, is a positive integer, The default is 1;-k strting,--worker-class strting: The working mode to use, the default is sync async, you can download Eventlet and gevent and specify the--threads INT: The number of worker threads to process the request, Runs each worker with a specified number of threads. is a positive integer, the default is 1. --worker-connections INT: The maximum number of client concurrency, by default this value is 1000. --backlog int: The maximum number of pending connections, that is, the number of customers waiting for the service. Default 2048, generally do not modify;-P file,--pid file: Sets the filename of the PID file, if not set will not create PID files--access-logfile file: The Access log directory to write--access-logformat STRING: The access log format to be written--error-logfile file,--log-file: The directory of files to write to the error log. --log-level Level: Error log output rating. --limit-request-line the maximum size of the number of rows int:http the request header, which is used to limit the allowable size of the HTTP request line, which by default is 4094. The value is a 0~8190 number. --limit-request-fields INT: Limits the number of request header fields in an HTTP request. This field is used to limit the number of request header fields to prevent a DDoS attack, by default, this value is 100, which cannot exceed 32768--limit-request-field-size INT: Limit the size of the request header in an HTTP request, By default, this value is 8190 bytes. The value is an integer or 0 when the value is 0 o'clock, which indicates that the request header size will not be limited to-t int,--timeout int: After so many seconds the work will be killed and restarted. Normally set to 30 seconds;--daemon: Whether to start with daemon, default false;--chdir: Switch directory before loading the application,--graceful-timeout INT: By default, this value is 30, at timeout (fromThe work that is still alive after the restart signal is received will be forcibly killed;--keep-alive INT: The number of seconds to wait on a keep-alive connection, by default, by a value of 2. The general setting is between 1-5 seconds. --reload: The default is False. This setting is used for development and causes the work to restart whenever the application changes. --spew: The print server executes every statement, false by default. This choice is atomic, that is, either print all or none;--check-config: Displays the current configuration with a default value of FALSE. -e env,--env env: setting environment variables;
Start as a configuration file
# gunicorn.conf# 并行工作进程数workers = 4# 指定每个工作者的线程数threads = 2# 监听内网端口5000bind = ‘127.0.0.1:5000‘# 设置守护进程,将进程交给supervisor管理daemon = ‘false‘# 工作模式协程worker_class = ‘gevent‘# 设置最大并发量worker_connections = 2000# 设置进程文件目录pidfile = ‘/var/run/gunicorn.pid‘# 设置访问日志和错误信息日志路径accesslog = ‘/var/log/gunicorn_acess.log‘errorlog = ‘/var/log/gunicorn_error.log‘# 设置日志记录水平loglevel = ‘warning‘
gunicorn -c gunicorn.conf app:app
Attention:
Startup occurs because the Windows platform does not support the successful installation of Gunicorn,gunicorn
' Importerror:no module named _curses/fcntl ' Error; need to download fcntl.py file;
Reference article:
78538188
Configuration of Python's Gunicorn