python web service development

python web service development

Since the project business needs, the next period of time is mainly done web service development. About python web service development there are two aspects to consider:

  • Implement the service
  • Performance testing services

The whole process service is to provide an interface for others to request, the server receives the request after the data ready and prepared results back to the requesting end. Panel adopts Flask + Gunicorn + Nginx overall program to achieve the deployment of service support, which focuses on need to know is Flask and Gunicorn, Nginx is responsible for operation and maintenance over there.

Nginx

Nginx (pronounced with engine x) is an asynchronous framework of the web server can also be used as a reverse proxy , load balancers and HTTP caching . The software developed by Igor Saisuoyefu created and in 2004 the first public release. The company of the same name was established in 2011 to provide support. March 11, 2019, Nginx company is F5 Networks acquired for $ 670 million.

Nginx is free and open source software , according to the class BSD license issued provisions. Most use a Web server Nginx, usually as a load balancer , the role of the following:

  • Nginx can live request cache, then the network initiates a request again, and you can configure load balancing, in the case of too much, load balancing can request to play if a multi-process machine (Gunicorn is a multi-process) to multiple machines
  • Alternatively Python Nginx processing request static files to improve performance
  • In many cases may be more than one service on a machine, you need this like Nginx web server to do a proxy_pass

The following are the basic framework:
Here Insert Picture Description

Gunicorn

Gunicorn is a WSGI server in Python, it sounds very ordinary, but because it supports many features, Python is still very popular in the community, such as from the parent process Flask, Flask support with gevent to make a patch and so on.

It uses pre-fork model, that is activated when the fork out n the process, then the master process is responsible for monitoring signals and the child, if the child hung up, then the master will pull a new up, if there is a corresponding signal, master It will initiate the appropriate action.

Gunicorn option to read from three places:

  • First specify the framework, there is only a framework called only Paste, we can ignore it
  • Second, from -c <path/to/configuration>reading the file specified in python
  • The last reading from the command line parameter in

In order from top to bottom, increasing precedence.

Detailed configuration is here: http: //docs.gunicorn.org/en/stable/settings.html

Flask

Flask is using a Python lightweight written Web application framework . Based Werkzeug WSGI toolkit and Jinja2 template engine . Flask using the BSD license.

Flask is called "microframework", because it uses simple core, increase other functions extension. Flask no database used by default, form validation tool. However, Flask retained the amplification of flexibility can Flask-extension adding these features: ORM , form validation tools, file upload, a variety of open authentication technology.

Implement the service

The main service program is based on Flask framework written in python program packaged offline with Flask package it, to support GET or POST request. E.g

from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello World!"

if __name__ == "__main__":
    app.run()

The above procedure was printed pages Hello World program, after the program started, the local input 0.0.0.1:5000/can occur displays Hello World page.

def hello()The main function is the main function of the package, off-line operation in this line, the rest of the frame are encapsulated Flask, facilitate the realization of an interface server.

Cache

connection pool

Since the project needs to read and write multi-threaded database, use the database connection pool connected in this way it would be better, otherwise prone to database transaction problems.

Database connection pool is responsible for the distribution, management and release of database connections, which allows an application to reuse an existing database connection, rather than re-create a; release the database connection idle for more than the maximum idle time to avoid because there is no release of the database connection database connection due to omissions. This technology can significantly improve the performance of database operations.

Performance Testing

Performance testing tools is the siege, there is no longer a detailed description, performance test results show the following:

  • Not use cache:
    Here Insert Picture Description
  • The use of caching:
    Here Insert Picture Description
  • The use of caching and connection pooling of the case:
    Here Insert Picture Description

It can be seen when using the cache, performance has been improved leaps and uses connection pooling, little change in performance, but will not have a problem things.

reference

Published 82 original articles · won praise 82 · views 240 000 +

Guess you like

Origin blog.csdn.net/uncle_ll/article/details/99757283