9 ways to fully interpret Python web programs
Python has a lot of web development frameworks, the code is finished, deployment on-line is a big deal, generally speaking, Web applications are generally three-tier structure
Web server---->application-----> DB Server
- Mainstream Web server can count on a slap, Apache,lighttpd,nginx,iis.
- Application, the Chinese name for the app service, is the app code you write based on a web framework
- DB server refers to the storage services, web development with more MySQL, in recent years because of the scale of the site, Memcache,redis this key-value and other storage also popular
Top-of-the- Web server has 3 features
- efficient processing of static files , Web server is developed with C, call is native function, the IO, file transfer are targeted optimization
- As a simple network firewall , you can Denny some IP, simply control the number of concurrent connections, and so on, better than nothing
- processing high Concurrent short connection requests , the request of thousands of users through the intranet dozens of long connections to forward, one reason is that Web server processing high concurrency is very professional, Another reason is that most of the frameworks used by application do not have the ability to handle high concurrency
In fact, there are some web frameworks in the market because of the built-in support epoll/kqueue and other efficient network library, but with the ability to handle high concurrency, such as the Python Tornado,java system tomcat,jetty and so on, Someone will get rid of the front-end Web server, directly naked, but in the deployment of public network applications, it is best not to do so, because the aforementioned 1, 22 reasons, user brower to Web server network condition is strange, you can not imagine,
Web server strongly recommends using Nginx for three reasons
- Excellent performance, very stable
- Simple installation, less dependent packages
- conf files are very easy to configure and easier than APACHE/LIGHTTPD
There are 9 ways to deploy a Python-developed web program
Mod_python, this is the Apache built-in module, very heavily dependent on Mod_python compiled using the Python version, and Apache companion use, not recommended
CGI, this is too old, not recommended, and Nginx does not support CGI mode, only with LIGHTTPD or Apache
fastcgi , this is currently the most popular practice, through the Flup module to support, in Nginx corresponding configuration instructions are Fastcgi_pass
spawn-fcgi, this is fastcgi multi-process management program, LIGHTTPD installation package comes with, and flup effect, the difference is Flup is the Python code-level introduction, SPAWN-FCGI is an external program. SPAWN-FCGI uses a wide range of code to support any language development, Php,python,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, but also the alternative version of CGI, SCGI protocol is very easy, I think and fastcgi almost, just not how to promote, nginx corresponding configuration command is scgi _pass, you want to use it, Flup also support.
http, Nginx use proxy_pass forwarding, this requires the backend appplication must be built to handle high-concurrency HTTP server, in the Python web framework, only select Tornado.
Python programmers like to invent wheels, tornado in addition to being a web framework, it can also provide high-performance HTTP server alone, so if you use other Python framework to write code, such as bottle, you can also pass import tornado
To start a high-performance HTTP server, the same can be deployed with the HTTP protocol and Nginx. Extension, Python packages can handle high-concurrency HTTP server There are many, such as gevent, can also be referenced by other frameworks to support the deployment of HTTP.
In reality, Java is used to do web programs, usually with HTTP and nginx mates, application Server select Tomcat or jetty
Uwsgi, consisting of 4 parts,
- UWSGI protocol
- Web server built-in Support protocol module
- Application Server protocol Support module
- Process Control Procedures
Nginx from 0.8.4 start built-in Support UWSGI protocol, UWSGI protocol is very simple, a 4 byte header+ a body,body can be a lot of protocol packages, such as http,cgi (through the header field), I used to do a small-scale performance comparison test, and the results showed that performance was less obvious than Uwsgi and fastcgi, and could be the reason for the smaller data set.
Uwsgi is characterized by its own process Control program. It is written in C, using the Natvie function, which is actually similar to SPAWN-FCGI/PHP-FPM. So UWSGI can support a variety of application frameworks, including (PYTHON,LUA,RUBY,ERLANG,GO), etc.
Gunicorn, and Uwsgi similar tools, migrated 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 defined at python2.5 (PEP 333), Root red Miaozheng, and deployment is relatively simple, http://gunicorn.org /On a detailed tutorial
Mod_wsgi, a module of Apache, is also supporting the WSGI protocol, https://code.google.com/p/modwsgi/
Comparison of FASTCGI protocol and HTTP protocol in code deployment
Although FASTCGI is a binary protocol, it does not conserve resources relative to the HTTP protocol. Binary protocol, can only save the expression of numbers, such as 1234567, with a string to represent the need for 7 byte, with a number is 4 byte, and the string to where all the same
FastCGI in the transfer of data, in order to be compatible with the CGI protocol, but also with a bunch of CGI environment variables, so compared with the HTTP protocol, with the fastcgi transmission of data does not save, but more
FastCGI the only advantage is that it is a long connection, the user concurrent 1000 request,fastcgi may be used 10 links to the back end of the appplication, if the HTTP protocol, how much to give, will be back to the end Appplication Initiate 1000 requests
HTTP proxy forwarding, in the face of ultra-high concurrency in the case of problems, because,TCP stack, port is int16 integer you create a local connect, you need to consume a port, up to 65536. External concurrent hundreds of thousands of requests, port pool dry, your server can only refuse to respond
Summarize
My personal habit is to use fastcgi protocol to deploy Python program, simple and easy, choose technical solution, must choose the simplest most common, this blog's fastcgi run script as follows
- Kill -9 ' Cat/tmp/django.pid '
- echo ' Restart Django ... '
- Python./Manage.PY runfcgi--Settings=Lutaf.Settings_r Maxchildren=8 maxspare=3 Minspare=1 Method==/tmp/django.pid host=127.0. 0.1 Port=9900 Outlog=< span class= "str" >/tmp/dj. Errlog=/tmp/dj.< Span class= "PLN" >error
Recommend everyone try Gunicorn, this is the future development direction
Tags: djangogunicornfastcgiwsgi
9 ways to fully interpret Python web programs