当前位置:网站首页>It's enough to read gunicorn's user manual [all used]

It's enough to read gunicorn's user manual [all used]

2021-12-03 23:54:47 Mainong Feige

Hello! , I'm Manon Feige , Thank you for reading this article , Welcome to three links with one button .
1. Take a stroll around the community , Weekly benefits , There are surprises every week . Manon Feige community , Leap plan
2. Python Basic column , Basic knowledge in a net . Python From entry to mastery
️ 3. Ceph actual combat , Everything from principle to actual combat . Ceph actual combat
Dry cargo is full. , Recommended collection , I need to use it. I need to look at it from time to time . If you have any questions and need , Welcome to leave a message ~ ~ ~.

Gunicorn What is it?

Gunicorn It's a Python Of WSGI HTTP The server . It is usually located in the reverse proxy ( Such as Nginx) perhaps Load balancing ( Such as AWS ELB) And a web application ( such as Django perhaps Flask) Between . It is a transplant from Ruby Of Unicorn Project pre-fork worker Model , Namely support eventlet Also support greenlet.
If the Flask The framework is still unclear , Check out this article Learn in a minute Flask Installation and quick use of frame
Gunicorn After starting the project, there must be a main process Master And one or more work processes . The number of work processes can be specified . The work process is the process that actually handles the request . The main process is to maintain the operation of the server .
 Insert picture description here

Gunicorn Installation

install

#  Install the latest version gunicorn
pip install gunicorn
# Install the specified version of gunicorn
pip install gunicorn==19.9.0
# Asynchronous mode 
pip install gevent==1.4.0

see Gunicorn Version of

pip show gunicorn

function Gunicorn

  gunicorn [OPTIONS] $(MODULE_NAME):$(VARIABLE_NAME)

$(MODULE_NAME):$(VARIABLE_NAME) Indicates the... To start WSGI_app.
among MODULE_NAME The corresponding is python file name ,VARIABLE_NAME Corresponding web Application example .
For example !!! Here is a definition called manage.py file , Create... Inside it Flask application .

#manage.py
from flask import Flask
def create_app():
    app = Flask(__name__)
    ....
    return app
app = create_app()
if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5001)

that , use gunicorn Start this Flask The command of the application is :

  gunicorn --workers=2 manage:app

among workers Used to specify the number of worker processes .
Of course, you can also call create_app Method

#manage.py
from flask import Flask
def create_app():
    app = Flask(__name__)
    ....
    return app

that , use gunicorn The command calls directly create_app The way is :

  gunicorn --workers=2 manage:create_app()

Gunicorn Description of configuration parameters [OPTIONS]

In actual development, a separate python Configuration file to configure gunicorn Start parameter of .gunicorn There are many parameters of , Here we will focus on some configurations that are often used in actual development .

1.1 The configuration file config

The order is :-c CONFIG or --config=CONFIG
This command specifies Gunicorn Configuration file for , The function of the configuration file is to write the configuration we need to the configuration file .
The default configuration file name is ./gunicorn.conf.py, Generally, the configuration file is placed in the root directory of the project . It looks like this
 Insert picture description here
Set up here gunicorn The binding of IP Address and port number ,worker Number and number of threads .
The command to start the project is : gunicorn -c gunicorn.conf.py manage:app.
 Insert picture description here

1.2 Debugging

command --reload=True, Or in gunicorn.conf.py Add to file reload=True To configure .
In this case , When the code changes gunicorn You can restart it .

1.3 Logging

Logging There are two kinds of logs , A business log info, One is the error log error. I'll add later .

1.4 Specify the process name

command -n proc_name perhaps --name=APP_NAME Is used to specify the gunicorn The name of the process
By default, through gunicorn After starting the project , The name of the process is gunicorn. It should be noted that the name of this process is different from the name of the application .
as follows :gunicorn -c gunicorn.conf.py --name=test_app manage:app The name of the specified process is test_app.
 Insert picture description here

1.5 Client address (server socket)

Command line -b ADDRESS perhaps --bind ADDRESS
default IP And the port number is 127.0.0.1:8000
bind Configuration is mainly used to specify the application binding IP And port number .
The format is HOST,HOST:PORT,unix:PATH perhaps fd://FD, It should be noted that IP The address must be a legal IP Address
for example :gunicorn -b 0.0.0.0:8111 manage:app. Specify the port number as 8111.
 Insert picture description here

1.6 Number of working processes (Worker Processes)

Command line -w INT perhaps --workers INT.
The default number of processes is 1.
This configuration is used to specify the number of worker processes that process requests , On a single core machine, this number is generally 2-4 Between the two . You need to find the number of processes that best fit the server .
for example :gunicorn -w 5 manage:app, Said to start 5 Working process . Each worker process can multithread tasks .
 Insert picture description here

1.7 Working mode (worker_class)

Command line -k STRING perhaps --worker-class STRING
The default working mode is sync, That is, synchronous working mode .
There are five working modes , Namely sync, eventlet, gevent, tornado, gthread .
These working modes are introduced below .

  1. sync Pattern ( Synchronous working mode )
    This is the most basic working mode , It is also the default working mode , Thread is native type . Request first come, first served , Queuing mode .
     Insert picture description here
  2. eventlet Pattern ( Coroutine asynchrony )
    eventlet The working mode is based on eventlet library , utilize python The realization of coprocessing .
    To use this working mode, you must first install eventlet library , And the version must be greater than or equal to 0.24.1
    The installation command is :pip install eventlet
  3. gevent Pattern ( Coroutine asynchrony )
    gevent Is based on Greentlet library , utilize python The realization of coprocessing .
    The installation command is :pip install gevent
    Gunicorn It is allowed to set the corresponding worker Class to use these asynchronies Python library . The settings here apply to what we want to run on a single core machine gevent:
gunicorn --worker-class=gevent -w 2  manage:app

 Insert picture description here
5. tornado Pattern
tornado utilize python Tornado Framework to implement . The installation command is :pip install tornado Installed tornado The version of the library must be greater than or equal to 0.2.
6. gthread Pattern
gthread It adopts thread working mode , Use thread pool to manage connections , Need to install gthread library .
The installation command is :pip install gthread.
Gunicorn Allow each worker Have multiple threads . In this case ,Python Every application worker Will be loaded once , The same worker Each generated thread shares the same memory space . In order to be in Gunicorn Using multithreading . We used gthreads Pattern , Appoint threads Parameters .

gunicorn --workers=5 --threads=2 main:app

The order is equivalent to :

gunicorn --workers=5 --threads=2 --worker-class=gthread main:app

The maximum number of concurrent requests in this example is worker* Threads , That is to say 10. Appoint threads Parameter, the working mode will automatically change to gthread Pattern .

1.8 Number of threads (threads)

Command line --threads INT
The default for each worker One thread execution .
Indicates the number of threads per worker process processing requests . Appoint threads Parameter, the working mode will automatically change to gthread Pattern .

1.9 Number of worker connections (worker_connections)

Command line :--worker-connections INT
The default value is : 1000
The meaning of this parameter is : Number of simultaneous connections per worker thread , This parameter is only used in Eventlet and Gevent Effective in both operating modes .

gunicorn --worker-class=gevent --worker-connections=1000 --workers=3 main:app

worker-connections Is for gevent worker Class .(2CPU)+1 Still recommended workers Number . Because we have only one core , We will use 3 individual worker. under these circumstances , The maximum number of concurrent requests is 3000.(3 individual worker1000 A connection /worker)

1.10 Maximum number of requests (max_requests)

Command line --max-requests INT
The default value is 0
The meaning of this parameter is : Before restarting the worker process (worker) Maximum number of requests that can be processed , Any one is greater than 0 The value of will limit the working process (worker) Number of requests processed before restart , This is a simple way to help limit memory leaks .
If the value is set to 0( The default value is ) Then the automatic restart of the working process is disabled

1.11 timeout (timeout)

Command line -t INT perhaps --timeout INT
The default value is 30 second .
If the worker process does not respond within the set timeout, it will be killed and restarted .
The duty of 0 This means that the timeout setting is disabled .
Usually , default 30 Seconds is enough , This value is modified only when it is determined that it has an impact on the synchronization worker process . For asynchronous worker processes , This means that the work process is still running , Not limited by the time it takes to process a single request .

1.12 The lifetime of the connection (keepalive)

Command line --keep-alive INT
The default value is 2
Connection timeout , Usually set at 1-5 In seconds .

Supplementary description of working mode

When worker Designated as gevent perhaps evenlet Type , Threads become based on Greentlet Of task( Pseudo thread ), At this time, the number of threads threads The parameter is invalid .

Use gevent There will be some compatibility problems with the mode .

Use gevent when , The system will use monkey patch. Some functions of the system will be modified ,
Some libraries will be compatible gevent The type of ,
for example , Task scheduling Library apscheduler,web socket need socketio Library, etc , You need to choose gevent Function of .
Some libraries cannot be used directly , For example, multi process multiprocess.
for example , In a api In request , If you need to use multi-core cpu resources , use multiprocess Perform multiprocess Computing . There will be a stuck problem .gevent in , Out of commission multiprocess library .

Performance practices

  1. If this application is I/O be limited to , You can usually use **“ Pseudo thread ”(gevent or asyncio) Working mode of ** To get the best performance . As we know ,Gunicorn By setting the appropriate worker Class and workers The quantity is adjusted to (2*CPU)+1 To support this programming paradigm .
  2. If this application is CPU be limited to , Then it doesn't matter how many concurrent requests the application handles , The only important thing is the number of parallel requests . because Python’s GIL, Threads and ’ Pseudo thread ’ Cannot execute in parallel mode , Can be worker Change the quantity of to CPU The number of nuclear , Understand that the maximum number of parallel requests is actually the number of cores . The appropriate working mode at this time is sync Working mode .
  3. If you are not sure about the memory footprint of your application , Use multithreading and the corresponding gthread worker Class will produce better performance , Because the application will be in every worker Load once on , And in the same worker Each thread running on shares some memory , But it takes some extra CPU Consume .
  4. If you don't know what you should choose, start with the simplest configuration , Just workers The quantity is set to (2*CPU)+1 And don't worry about Multithreading . From this point on , Is the benchmark environment for all tests and errors . If the bottleneck is in memory , Start introducing multithreading . If the bottleneck is I/O On , Consider using different Python Programming paradigm . If the bottleneck is CPU On , Just consider adding more kernels and adjusting workers Number .

summary

This article introduces in detail Gunicorn The installation and use of and the meaning of various configuration parameters . I hope it can help readers . For these studies, it is best to read the official documents .

Exclusive benefits for fans

Soft test materials : Practical soft test materials

Interview questions :5G Of Java High frequency interview questions

Learning materials :50G All kinds of learning materials

Off order secret script : reply 【 Take off a single 】

Concurrent programming : reply 【 Concurrent programming 】

											  Verification Code   You can search below   official account   obtain  

Reference resources

Gunicorn Introduce
[ translate ] By optimizing the Gunicorn Configuration improves performance
Gunicorn Official documents

版权声明
本文为[Mainong Feige]所创,转载请带上原文链接,感谢
https://chowdera.com/2021/11/20211116180543406C.html

随机推荐