Web deployment topic (1): Gunicorn operation and configuration method

Gunicorn "Green Unicorn" is a widely used high-performance Python WSGI UNIX HTTP server, ported from Ruby's Unicorn (Unicorn) project, using the pre-fork worker mode, has a very simple and lightweight to use Features such as resource consumption and high performance.

Install 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 full path + python file name; the variable name is the callable WSGI (Web Server Gateway) in the python file.

Examples:

# 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 the 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 actually puts this service in the background to run.

-w WORKERS, --workers=WORKERS

The number of work processes. As mentioned above, gunicorn is a pre-fork worker mode, which means that when gunicorn starts, a specified number of worker processes will be pre-forked in the main process. When processing requests, gunicorn relies on the operating system to provide load balancing. Workers are usually recommended. The quantity is: (2 x $ num_cores) + 1

-k WORKERCLASS, --worker-class=WORKERCLASS

Worker process type. Includes sync (default), eventlet, gevent, or tornado, gthread, gaiohttp.

--backlog INT

The maximum number of pending connections.

--chdir

Switch to the specified working directory.

--log-level LEVEL

The granularity of the output error log, valid LEVEL are:

debug
info
warning
error
critical
--access-logfile FILE

Confirm the file FILE to be written to the Access log. '-' Means output to standard output.

--error-logfile FILE, --log-file FILE

Confirm the file FILE to be written to the error log. '-' Means output to standard error output.

gunicorn configuration

Gunicorn gets configuration from three different places:

Frame settings (usually only affects Paster applications)

Configuration file (python file): The configuration in the configuration file will override the settings of the framework.

Command Line

The framework settings are only related to Paster (a web framework), not discussed; the command line configuration is shown in the above section; now we see how to configure gunicorn with configuration files:

The configuration file must be a python file, just write the parameters on 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. E.g:

# 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, the configuration file can also achieve more complex configuration:

# Gunicorn.py 
Import the logging
 Import logging.handlers
 from logging.handlers Import WatchedFileHandler
 Import OS
 Import multiprocessing 
the bind = ' 127.0.0.1:8000 '    # binding ip and port 
backlog = 512         # listen queue 
the chdir = ' / Home / Test / server / bin '  # gunicorn to switch to the target working directory 
timeout = 30    # timeout 
worker_class = ' gevent '  # use gevent mode, you can also use sync mode, the default is sync mode
= multiprocessing.cpu_count Workers () * 2 + 1   # number of processes 
Threads = 2 # specifies the number of threads per process open 
the LogLevel = ' info '  # log level, the log level refers to the level of the error log, and access log The level 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 gunicorn access log format, the error log can not be set 
"" " 
meaning that each option is as follows: 
H Remote address 
L '-' 
U Currently '-', On May BE User name Future Releases in 
T date of the request 
r status line (eg `` GET / HTTP / 1.1``) 
s status 
b response length or '-'
f     referer
User Agent A
Request Time in seconds The T 
D Request Time in microseconds 
L decimal Request Time in seconds The 
P Process ID 
"" " 
the 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

 

Guess you like

Origin www.cnblogs.com/qiu-hua/p/12680905.html