ubuntu install flask + nginx + gunicorn TBD

first step

Check the server environment pip python3 mysql redis can, on the next, the upgrade to upgrade

 

The second step

If your flask programs use the address on github git clone download it (if you need a private account and password, git recent study gave me out complicated)

In short text flask find a way to download the program to the server

 

third step

Install virtual environment virtualenv also use pip install virtualenv

Create a folder

mkdir py3 (random name) 

cd into the file

cd py3

 

Virtualenv use to create a virtual environment 

virtualenv - the p-env python (python virtual environment to specify the name of the 3 versions of ubuntu if you do not set the default is likely p2 version) 

 

Source / file you created folder / bin / of an activate enter into the virtual environment 

(exit is deactivate command )

 

 

 

the fourth step

It has entered into the virtual environment using git clone file download folder (I assume you still rely on packaged files using pip freeze)

pip install -r reqxxxxx.txt

 

Installation and operation of this flask packages

 

Check the flask configuration information, such as address redis database, mysql address, (I did not set configured in the system variables in ubuntu, are expressly written in code)

If the plug flask_script and flask_migrate 

Then the establishment of a database (migrate only help build the table, not building a database) 

python flask the init entry file DB 

python flask inlet the migrate file DB 

python flask entry file db upgrade

 

 

 

the fifth step

Use gunicorn open multiple threads to run

Note that if you import documents in development is the use of flask_script flask to operate the program, uploaded to the server and not removed, the removal of select, use the factory mode app = create_app () can use the service to get a start gunicorn

manage.py
from
app import create_app from flask_script import Manager from flask_migrate import MigrateCommand app = create_app() if __name__ == '__main__': app.run() #manage = Manager(app=create_app()) #manage.add_command('db',MigrateCommand) #if __name__ == '__main__': # manage.run() ~

Let xxx gunicorn is likely to occur only use a parameter can not use two parameters such tips

 

The command to start 

the test 
gunicorn -w 4 -b 0.0.0.0:5001 the Manage: App 

If you need to use the background daemon hang hang back to kill the process using ps -efl | grep gunicorn number inquiry process to terminate the process kill -9 xxxx
gunicorn -w 4 -D -b 0.0.0.0:5001 manage:app

The meaning of the above command is

gunicorn use -w open four process -b is my designated port 0.0.0.0:5001 so that others can be on your server public network ip: 5001 flaskweb access to this program is to start manage the name of the file entry careful not to suffix examples of the flask is app

 

It is possible to keep retrying process four or five times fail to display, belonging to the port is occupied, the port occupied kill kill -9 xxx when you start or change a

 

In addition to this there is a method to boot, the boot configuration file

gunicorn -c configuration file name: app instance

After setting the startup configuration file in this manner but looks like some parameters and spuervisor be related to a comment

# Gunicorn.conf 

# concurrent worker processes 
4 Workers = # The number of threads per worker designated 
Threads = 2 # inner listening ports 5000 
the bind = ' 0.0.0.0:5000 ' # daemon, the process to the supervisor management 
= daemon ' false ' # work mode coroutine 
worker_class = ' gevent ' comment out # this    # set the maximum amount of concurrent 
worker_connections = 2000 # to set the process directory 
the PidFile = ' /var/run/gunicorn.pid ' # set access and error logs information logpath 
the accesslog = ' /var/log/gunicorn_acess.log '







errorlog = ' /var/log/gunicorn_error.log ' 
# set the logging level 
LogLevel = ' warning '

 

 

 


 

At present simple test it, use multiple threads access the flask 500 reptile program ($ 10 Tencent cloud 1 nuclear 2g memory of the server, because it is write api, so there is no valid and static files),

And simply log 10 characters each write records to mysql, the test average is probably around 4.35s (not strict)

4.389250993728638

4.4822564125061035

4.46025538444519

4.415252685546875

4.415252447128296

4.60326361656189

 

This is a simple written test, but another problem comes if I had written it also returns the data?

 

Then select str (os.urandm (500)) in FIG flask knowledge generating function 500 returns a string format jsonify json 

There is a problem, as long as more than 500 threads reptiles, sent to 499 will stop moving, but the data added to the database also have added but is stuck at 499

Is mysql link quantitative restrictions? Reptile nor ah send data out returns are not 200 light sql problem, ah?

Pending solve this

 

However, after the transmission time 500 may determine packet data and the reception data 500 at the time of character data required rough average of about 12.5s

 However, if the return to only 20 characters generated by os.urandom (20) is an average value of about 4.4 

4.419252634048462

4.472255706787109

4.437253952026367

 

 Os.urandom may be due to the generation time consuming resources required string, it returns more slowly, a fixed value 500 same copy a string and returns the obtained result is the average of 4.5

4.7312705516815186

4.669267177581787

4.546260118484497  
4.408251762390137

 



 

 If this server cpu occupancy request has been saved in the proportion of 76 cpu

If the proportion of the end 33 into the                       

No access to accounting for 5


 

After the reverse proxy nginx and regulatory services supervisor settings, I learned to say TBD 

 

Guess you like

Origin www.cnblogs.com/zengxm/p/11241802.html