Home  >  Article  >  Backend Development  >  Summary of 9 deployment methods of python web programs

Summary of 9 deployment methods of python web programs

不言
不言Original
2018-04-16 17:01:521554browse

Python has many web development frameworks. After the code is written, deployment and launch is a big deal. Generally speaking, web applications are generally three-tier structure web server ---->application -----> DB server

You can count the mainstream web servers with one slap, apache, lighttpd, nginx, iis

application, the Chinese name is called application service, which is the application code you write based on a certain web framework DB server general Refers to storage service. MySQL is mostly used in web development. In recent years, due to the expansion of website scale, key-value storage such as memcache and redis has also become popular.
The web server placed at the front has 3 functions

Highly efficient processing of static files, the web server is developed in C, and the calls are native functions, with targeted optimization of IO and file transfer

Acts as a simple network firewall, you can denny Some IPs, simply controlling the number of concurrent connections, etc. are better than nothing

Handling high concurrent short connection requests, forwarding requests from thousands of users through dozens of long connections on the intranet, one reason is The web server is very professional in handling high concurrency. Another reason is that the frameworks used by most applications do not have the ability to handle high concurrency

In fact, some web frameworks on the market have built-in support for epoll/kqueue, etc. Efficient network libraries have the ability to handle high concurrency, such as Python's tornado, Java-based tomcat, jetty, etc. Some people remove the front-end web server and run naked. However, when deploying public network applications, it is best not to In this way, because of the two reasons mentioned above 1 and 2, the network conditions from the user browser to the web server are all kinds of strange, which you can't imagine.

Web server strongly recommends using nginx for three reasons

Excellent performance, very stable
Easy to install, few dependent packages
The conf file is very easy to configure, simpler than apache/lighttpd
There are 9 ways to deploy web programs developed in python

mod_python, this is a built-in module of apache. It depends heavily on the python version mod_python is compiled and used with. It is not recommended for use with apache.

cgi is too old and not recommended, and nginx does not support cgi. Method, you can only use lighttpd or apache

fastcgi. This is the most popular method at present. It is supported by the flup module. The corresponding configuration instruction in nginx is fastcgi_pass

spawn-fcgi , this is the fastcgi multi-process management program, which comes with the lighttpd installation package. It has the same effect as flup. The difference is that flup is introduced at the python code level, and spawn-fcgi is an external program. spawn-fcgi is very versatile and can support code developed in any language, such as php, python, and perl. As long as your code implements the fastcgi interface, it can help you manage your process.

scgi, the full name is Simple Common Gateway Interface is also an alternative version of cgi. The scgi protocol is very simple. I think it is similar to fastcgi, but it has not been widely promoted. The corresponding configuration command of nginx is scgi_pass. You can use it if you want. Flup also supports it.

http, nginx uses proxy_pass forwarding. This requires that the back-end application must have a built-in http server that can handle high concurrency. In the python web framework, you can only choose tornado.

Python programmers I like to invent the wheel. In addition to being a web framework, tornado can also provide a high-performance http server independently. Therefore, if you use other python frameworks to write code, such as bottle, you can also start a high-performance one by importing tornado. http server can also be deployed using http protocol and nginx. Expanding, there are many http servers in the python package that can handle high concurrency, such as gevent, which can also be referenced by other frameworks to support http deployment.

In reality, when using java to make web programs, http and nginx are usually used. The application server chooses tomcat or jetty

uwsgi, which consists of 4 parts,

uwsgi Protocol
Web server built-in support protocol module
application server protocol support module
Process control program

nginx has built-in support for the uwsgi protocol starting from 0.8.4. The uwsgi protocol is very simple, with 4 words each Section header is a body. The body can be packages of many protocols, such as http, cgi, etc. (marked by fields in the header). I once did a small-scale performance comparison test. The results showed that compared with fastcgi, uwsgi has no performance. The obvious advantage may also be due to the smaller data set

uwsgi is characterized by its own process control program. It is written in C language and uses the natvie function. In fact, it is the same as spawn-fcgi/php- fpm is similar. Therefore, uwsgi can support a variety of application frameworks, including (python, lua, ruby, erlang, go), etc.

Gunicorn, a tool similar to uwsgi, is transplanted from the rails deployment tool (Unicorn). But the protocol it uses is WSGI, the full name is Python Web Server Gateway Interface, which is the official standard (PEP 333) defined in python2.5. It has good roots and is relatively simple to deploy. http://gunicorn.org/ There is a detailed tutorial

mod_wsgi, a module of Apache that also supports the WSGI protocol, https://code.google.com/p/modwsgi/

fastcgi protocol and http protocol are used in code deployment Comparison of the advantages and disadvantages

Although fastcgi is a binary protocol, it does not save resources compared to the http protocol. The binary protocol can only save the expression of numbers, such as 1234567. It takes 7 Bytes to express it as a string, and 4 Bytes as a number, and the string is the same everywhere

When fastcgi transmits data, in order It is compatible with the cgi protocol and also brings a bunch of cgi environment variables. Therefore, compared with the http protocol, using fastcgi to transmit data is not economical, but more efficient.

The only advantage of fastcgi is that it is a long-term connection. , if the user makes 1000 concurrent requests, fastcgi may use 10 links to forward them to the back-end application. If you use the http protocol, you can give as many as you want, and 1000 requests will be initiated to the back-end application

http proxy forwarding method , problems will arise when facing ultra-high concurrency, because in the tcp protocol stack, port is an int16 integer. When you create a new connect locally, you need to consume a port, which can be up to 65536. There are hundreds of thousands of external concurrent requests, the port pool is exhausted, and your server can only refuse to respond.

Summary

My personal habit is to use the fastcgi protocol to deploy python programs. It is simple and trouble-free. Choose technology Solution, be sure to choose the simplest and most common one. The fastcgi running script of this blog is as follows

kill - `cat / tmp / django.pid`
echo 'restart django....' 
python . / manage.py runfcgi - - settings = lutaf.settings_r maxchildren =  maxspare = minspare =  method = prefork pidfile = / tmp / django.pid host = 127.0 . 0.1  port = outlog = / tmp / dj.out errlog = / tmp / dj.error

It is recommended that everyone try Gunicorn. This is the future development direction

The above is the detailed content of Summary of 9 deployment methods of python web programs. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn