Home Backend Development Python Tutorial Summary of 9 deployment methods of python web programs

Summary of 9 deployment methods of python web programs

Apr 16, 2018 pm 05:01 PM
python web Way

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
Copy after login

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 of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

Python vs. JavaScript: The Learning Curve and Ease of Use Python vs. JavaScript: The Learning Curve and Ease of Use Apr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

Can vs code run in Windows 8 Can vs code run in Windows 8 Apr 15, 2025 pm 07:24 PM

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

Can visual studio code be used in python Can visual studio code be used in python Apr 15, 2025 pm 08:18 PM

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

PHP and Python: A Deep Dive into Their History PHP and Python: A Deep Dive into Their History Apr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

How to run programs in terminal vscode How to run programs in terminal vscode Apr 15, 2025 pm 06:42 PM

In VS Code, you can run the program in the terminal through the following steps: Prepare the code and open the integrated terminal to ensure that the code directory is consistent with the terminal working directory. Select the run command according to the programming language (such as Python's python your_file_name.py) to check whether it runs successfully and resolve errors. Use the debugger to improve debugging efficiency.

Is the vscode extension malicious? Is the vscode extension malicious? Apr 15, 2025 pm 07:57 PM

VS Code extensions pose malicious risks, such as hiding malicious code, exploiting vulnerabilities, and masturbating as legitimate extensions. Methods to identify malicious extensions include: checking publishers, reading comments, checking code, and installing with caution. Security measures also include: security awareness, good habits, regular updates and antivirus software.

See all articles