Gunicorn "green Unicorn" is a widely used high-performance Python WSGI UNIX HTTP server. It is transplanted from the Unicorn project of Ruby and uses the pre fork worker mode. It has the characteristics of very simple use, lightweight resource consumption and high performance.
Installing gunicorn
$ sudo apt-get update $ sudo apt-get install gunicorn
Run gunicorn:
$ gunicorn [OPTIONS] Module name: variable name
The module name is the python file name, which can be the complete path + Python file name; The variable name is the callable WSGI (Web Server Gateway) in the python file
Example:
# filename: test.py def app(environ, start_response): """Simplest possible application object""" data = 'Hello, World!\n' status = '200 OK' response_headers = [ ('Content-type','text/plain'), ('Content-Length', str(len(data))) ] start_response(status, response_headers) return iter([data])
Run app:
$ gunicorn --workers=2 test:app
Common configuration parameters:
-c CONFIG, --config=CONFIG
Specify a configuration file (py file)
-b BIND, --bind=BIND
Bind with the specified socket
-D, --daemon
Running the Gunicorn process in the form of a daemon is actually putting the service in the background.
-w WORKERS, --workers=WORKERS
Number of work processes. As mentioned above, gunicorn is a pre fork worker mode, which means that when gunicorn is started, a specified number of worker processes will be forked in advance in the main process. When processing requests, gunicorn relies on the operating system to provide load balancing. Generally, the recommended number of workers is: (2 x $num_cores) + 1
-k WORKERCLASS, --worker-class=WORKERCLASS
Work process type. Including sync (default), eventlet, gevent, or tornado, gthread, gaiohttp
--backlog INT
Maximum number of pending connections
--chdir
Switch to the specified working directory
--log-level LEVEL
Output the granularity of error log. The effective levels are:
debug info warning error critical --access-logfile FILE
Confirm the FILE to be written to the Access log. FILE. '-' indicates output to standard output
--error-logfile FILE, --log-file FILE
Confirm the FILE to be written to the Error log. FILE. '-' indicates output to standard error output
gunicorn configuration
Gunicorn gets the configuration from three different places:
Frame settings (usually only affect Paster applications)
Configuration file (python file): the configuration in the configuration file overrides the settings of the framework.
command line
Framework settings are only related to Paster (a Web framework) and are not discussed; The command line configuration is shown in the above section; Now let's see how to configure gunicorn with a configuration file:
The configuration file must be a python file, just write the parameters in the command line into the py file. If you need to set which parameter, you can assign a value to the parameter in the py file. For example:
# example.py bind = "127.0.0.1:8000" workers = 2
Run gunicorn:
$ gunicorn -c example.py test:app
Equivalent to:
$ gunicorn -w 2 -b 127.0.0.1:8000 test:app
Of course, configuration files can also achieve more complex configurations:
# gunicorn.py import logging import logging.handlers from logging.handlers import WatchedFileHandler import os import multiprocessing bind = '127.0.0.1:8000' #Bind ip and port numbers backlog = 512 #listen queue chdir = '/home/test/server/bin' #gunicorn the destination working directory to switch to timeout = 30 #overtime worker_class = 'gevent' #Using gevent mode, you can also use sync mode, which is the default workers = multiprocessing.cpu_count() * 2 + 1 #Number of processes threads = 2 #Specifies the number of threads open per process loglevel = 'info' #Log level refers to the level of error log, but the level of access log cannot be set access_log_format = '%(t)s %(p)s %(h)s "%(r)s" %(s)s %(L)s %(b)s %(f)s" "%(a)s"' #Set the format of gunicorn access log. The error log cannot be set """ The meaning of each option is as follows: h remote address l '-' u currently '-', may be user name in future releases t date of the request r status line (e.g. ``GET / HTTP/1.1``) s status b response length or '-' f referer a user agent T request time in seconds D request time in microseconds L request time in decimal seconds p process ID """ accesslog = "/home/test/server/log/gunicorn_access.log" #Access log file errorlog = "/home/test/server/log/gunicorn_error.log" #Error log file
Run gunicorn:
$ gunicorn -c gunicorn.py test:app
This article comes from the blog Park, author: Qiu Hua. Please indicate the link of the original text for Reprint: https://www.cnblogs.com/qiu-hua/p/12680905.html