How to deploy 9 kinds of Python Web programs summary _python

Source: Internet
Author: User

Mainstream Web Server A slap can count out, Apache,lighttpd,nginx,iis

Application, Chinese name is to do application service, you are based on a Web framework written application code DB server refers to storage services, Web development in the use of MySQL more, in recent years because the scale of the site, Memcache, Redis This kind of key-value storage is also popular
There are 3 features in the front of the Web server

Efficient processing of static files, Web server is developed in C, call is native function, to Io, file transmission are targeted optimization

Act as a Simple network firewall, you can denny some IP, simple control the number of concurrent connections, and so on, better than nothing

Handle high concurrent short connection requests, the request of thousands of users through the intranet dozens of long connection forwarding, because one is the Web server processing high concurrency is very professional, another reason is that most of the application frame is not equipped with the ability to handle high concurrency

In fact, some of the web frameworks on the market have the ability to handle high concurrency, such as the tomcat,jetty of Python's Tornado,java system, because of the built-in support for efficient network libraries such as Epoll/kqueue, etc. Someone will remove the front end of the Web server, direct streaking, but in the deployment of public network applications, it is best not to do so, because the previous 1, 22 reasons, the user brower to the Web server network situation is strange, you can not imagine,

Web server strongly recommends using Nginx for three

Excellent performance, very stable
Simple installation, less dependent packets
conf file is very easy to configure, more simple than apache/lighttpd
There are 9 ways to deploy a Web program developed by Python

Mod_python, this is Apache built-in module, very serious relies on Mod_python compile use of Python version, and Apache supporting use, not recommended

CGI, this is too old, not recommended, and Nginx does not support the CGI method, only with LIGHTTPD or Apache

FastCGI, this is currently the most popular practice, through the Flup module to support, in the nginx corresponding configuration instructions are Fastcgi_pass

SPAWN-FCGI, this is fastcgi multiple process management program, LIGHTTPD installation package, and Flup effect, the difference is that the Flup is Python code-level introduction, SPAWN-FCGI is an external program. SPAWN-FCGI is very versatile, can support the development of any language code, Php,python,perl, as long as you code to achieve the FastCGI interface, it can help you manage your process

SCGI, the full name is simple Common Gateway Interface, but also the replacement version of CGI, SCGI protocol is very easy, I think and fastcgi almost, but not how to promote, nginx the corresponding configuration instructions are Scgi_pass, If you want to use it, Flup also support it.

HTTP, Nginx uses Proxy_pass forwarding, which requires that the backend appplication must have a built-in HTTP server that handles high concurrency, and that in the Python web framework, only tornado can be selected.

Python programmers like to invent wheels, tornado in addition to being a web framework, it can provide high-performance HTTP server alone, so if you use other Python framework to write code, such as bottle, you can also through the import Tornado to start a high-performance HTTP server, the same can be deployed with the HTTP protocol and Nginx. Extended, the Python package can handle the high concurrency of HTTP server There are many, such as gevent, can also be referenced by other frameworks to support the HTTP deployment.

In reality, Java is used as a Web program, usually with HTTP and Nginx, and the application server chooses Tomcat or jetty

Uwsgi, consisting of 4 parts,

UWSGI protocol
Web server built-in Support Protocol module
Application Server protocol Support module
Process Control Program

Nginx from 0.8.4 the built-in support UWSGI protocol, UWSGI protocol is very simple, a 4 bytes header+ a body,body can be a lot of protocol packages, such as http,cgi, etc. (through the header inside the field marked), I used to do a small performance comparison test, the results show that UWSGI and fastcgi compared to the performance of less obvious advantages, may be the reason for the smaller data sets

Uwsgi is characterized by its own process Control program. It is written in C language, using the Natvie function, in fact, and spawn-fcgi/php-fpm similar. So UWSGI can support a variety of application frameworks, including (PYTHON,LUA,RUBY,ERLANG,GO), etc.

Gunicorn, a similar tool with Uwsgi, is ported from the Rails Deployment Tool (unicorn). But the protocol it uses is WSGI, the full name of the Python Web Server Gateway Interface, the official standard defined at python2.5 (PEP 333), the root red Miaozheng, and the simpler deployment, http://gunicorn.org /a detailed tutorial on

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

Comparison of the advantages and disadvantages of FASTCGI protocol and HTTP protocol in code deployment

Although FASTCGI is a binary protocol, it does not save resources relative to the HTTP protocol. Binary protocol, can only save the expression of a number, such as 1234567, with a string representation of 7 byte, with the number is 4 byte, and the string to where all the same

FastCGI in the transmission of data, in order to compatible with the CGI protocol, but also with a bunch of CGI environment variables, so compared with the HTTP protocol, the use of FASTCGI data transmission is not a province, but more

FastCGI the only advantage is that it is a long connection, the user concurrent 1000 request,fastcgi may be 10 links to the back end of the appplication, if the HTTP protocol, how much to how much, will be back to the end Appplication 1000 requests launched

HTTP proxy forwarding, in the face of ultra-high concurrency will be a problem, because, TCP protocol stack, port is int16 integer type you create a new connect, need to consume a port, up to 65536. External concurrent hundreds of thousands of requests, port pool consumption, your server can only refuse to respond

Summarize

My personal habit is to use the FASTCGI protocol to deploy the Python program, Simple and easy, select the technical solution, must choose the simplest and most common, this blog's fastcgi run the script as follows

Kill-' cat/tmp/django.pid '
echo ' restart Django ... ' 
python./manage.py runfcgi--Settings = Lutaf.settin  Gs_r Maxchildren =  Maxspare = Minspare = Method  = prefork Pidfile =/tmp/django.pid host = 127.0.0.1  Port = Outlog =/Tmp/dj.out ErrLog =/tmp/dj.error

Recommend everybody try Gunicorn, this is the future development direction

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.