In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces the deployment of Web in Python, which has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, let the editor take you to know about it.
Don't let the server run naked.
Anyone who has studied PHP knows that the deployment of the formal environment of php is very simple. It takes a minute to change a few documents to OK and to use FastCgi. In comparison, the deployment of Python in web applications is much more complicated, mainly due to a wide range of tools and insufficient support on mainstream servers. Before understanding the deployment of Python in the production environment, let's clarify some concepts! It's important!
CGI:
CGI, the Universal Gateway Interface (Common Gateway Interface), is the interface standard between external applications (CGI programs) and Web servers, and the procedure for transmitting information between CGI programs and Web servers. The CGI specification allows Web servers to execute external programs and send their output to Web browsers, and CGI turns a simple set of static hypermedia documents from Web into a complete new interactive media. Generally speaking, CGI is like a bridge, connecting the web page with the execution program in the WEB server. It passes the instructions received by HTML to the server execution program, and then returns the results of the server execution program to the HTML page. The cross-platform performance of CGI can be implemented on almost any operating system.
CGI mode first creates a child process of cgi when it encounters a connection request (user request), activates a CGI process, then processes the request, and ends the child process after processing. This is the fork-and-execute mode. Therefore, the server in cgi mode will have as many CGI child processes as there are connection requests, and repeated loading of child processes is the main reason for the poor performance of cgi. When the number of user requests is very large, it will occupy a large number of system resources such as memory, CPU time and so on, resulting in low performance.
CGI script workflow:
Hongmeng official Strategic Cooperation to build HarmonyOS Technology Community
The browser points to the URL of a CGI application through an HTML form or hyperlink request.
The server executes the server to send and receive requests. The specified CGI application.
CGI applications perform the required actions, usually based on what the viewer enters.
CGI applications format the results into documents (usually HTML pages) that web servers and browsers can understand.
The web server returns the results to the browser.
Python has a cgi module to support native cgi programs
FastCGI:
FastCGI is a scalable, high-speed communication interface between HTTP server and dynamic scripting languages. Most popular HTTP server supports FastCGI, including Apache, Nginx, and lighttpd, while FastCGI is also supported by many scripting languages, including Python. FastCGI is developed and improved from CGI. The main disadvantage of the traditional CGI interface is poor performance, because every time the HTTP server encounters a dynamic program, it needs to restart the script parser to perform parsing, and the result is returned to the HTTP server. This is almost unavailable when dealing with highly concurrent access. FastCGI is like a resident (long-live) CGI that can be executed all the time, as long as it is activated and does not take the time to fork every time (this is the most criticized fork-and-execute mode of CGI). CGI is the so-called short-lifetime application, and FastCGI is the so-called long-lifetime application. Because the FastCGI program does not need to generate new processes constantly, it can greatly reduce the pressure on the server and produce high application efficiency. Its speed efficiency is at least five times higher than that of CGI technology. It also supports distributed computing, that is, FastCGI programs can be executed on hosts other than web servers and accept requests from other web servers.
FastCGI is a language-independent, scalable architecture open extension of CGI, and its main behavior is to keep the CGI interpreter process in memory and thus achieve high performance. As we all know, the repeated loading of the CGI interpreter is the main reason for the poor performance of CGI. If the CGI interpreter remains in memory and accepts scheduling by the FastCGI process manager, it can provide good performance, scalability, Fail-Over features, and so on. The FastCGI interface adopts Cramp S structure, which can separate the HTTP server from the script parsing server, and start one or more script parsing daemons on the script parsing server. Every time the HTTP server encounters a dynamic program, it can deliver it directly to the FastCGI process for execution, and then return the results to the browser. This approach allows the HTTP server to handle static requests exclusively or return the results of the dynamic script server to the client, which greatly improves the performance of the entire application system.
The workflow of FastCGI:
Hongmeng official Strategic Cooperation to build HarmonyOS Technology Community
Load the FastCGI process Manager (PHP-CGI or PHP-FPM or spawn-cgi) when Web Server starts
The FastCGI process manager initializes itself, starts multiple CGI interpreter processes (see multiple php-cgi) and waits for a connection from Web Server.
When the client request arrives at Web Server, the FastCGI process manager selects and connects to a CGI interpreter. Web server sends CGI environment variables and standard input to the FastCGI child process php-cgi.
The FastCGI child process returns standard output and error messages from the same connection to Web Server after processing. When the FastCGI child process closes the connection, the request is processed. The FastCGI child process then waits and processes the next connection from the FastCGI process manager (running in Web Server). In CGI mode, php-cgi exits here.
Characteristics of FastCGI:
Break the traditional page processing technology. In the traditional page processing technology, the program must be on the same server as the Web server or the Application server. This kind of history has been broken by FastCGI technology N years ago. The application of FastCGI technology can be installed on any server in the server farm, and communicate with Web server through TCP/IP protocol. This is not only suitable for the development of large-scale distributed Web group, but also suitable for efficient database control.
A clear request pattern. CGI technology does not have a clear role, in the FastCGI program, the program is given a clear role (responder role, authenticator role, filter role).
WSGI:
The Python Web server gateway interface (Python Web Server Gateway Interface, abbreviated as WSGI) is a simple and general interface between a Web server and a Web application or framework defined for the Python language. Since WSGI was developed, similar interfaces have appeared in many other languages. WSGI is used as a low-level interface between Web server and Web application or application framework to enhance the common ground of portable Web application development. WSGI is designed based on existing CGI standards.
The WSGI area is divided into two parts: one is "server" or "gateway", and the other is "application" or "application framework". When processing a WSGI request, the server provides the application with an environment context and a callback function (Callback Function). When the application finishes processing the request, the result is passed back to the server through the previous callback function. The so-called WSGI middleware implements both sides of API, so it can mediate between WSGI services and WSGI applications: from the WSGI server's point of view, the middleware acts as the application, while from the application's point of view, the middleware acts as the server. The "middleware" component can perform the following functions:
Hongmeng official Strategic Cooperation to build HarmonyOS Technology Community
After overriding the environment variable, the request message is routed to different application objects according to the target URL.
Allows multiple applications or application frameworks to run simultaneously in a process.
Load balancing and remoting, by forwarding request and response messages over the network.
Perform post-processing of content, such as applying XSLT style sheets.
In the past, how to choose the right Web application framework has become a problem for Python beginners because, in general, the choice of Web application framework will limit the choice of available Web servers, and vice versa. At that time, Python applications were usually designed for one of the CGI,FastCGI,mod_python, or even for the custom API interface of a specific Web server. There is no official implementation of WSGI because WSGI is more like a protocol. WSGI applications (Application) can run on any server (Server) as long as these protocols are followed, and vice versa. WSGI is the CGI packaging of Python, and relative to Fastcgi is the CGI packaging of PHP.
WSGI divides web components into three categories: web server, web middleware, web application. The basic processing mode of wsgi is: WSGI Server-> (WSGI Middleware) *-> WSGI Application.
Uwsgi:
Uwsgi protocol is an inherent protocol of uWSGI server, which is used to define the type of transmission information (type of information). Each 4byte before uwsgi packet is a description of the type of transmission information, which is different from WSGI. It is said to be 10 times more efficient than fcgi. For more information on the protocol, please see the uwsgi protocol (http://uwsgi-docs.readthedocs.org/en/latest/Protocol.html)).
The above four can be understood as agreements! Agreement! Agreement! With such a protocol, you can implement the web service associated with the Web server and the Web application!
UWSGI:
The uWSGI project aims to develop a complete solution for network applications that deploy distributed clusters. UWSGI is mainly for web and its standard services, and has been successfully applied to many different languages. Because of uWSGI's extensible architecture, it can be used by * * system extensions to support more platforms and languages. Currently, you can write plug-ins using CMagna Category + and Objective-C. The "WSGI" in the project name is to thank the Python Web standard of the same name, because WSGI has developed * plug-ins for the project. UWSGI is a Web server, which implements WSGI protocol, uwsgi, http and other protocols. UWSGI, neither wsgi nor FastCGI protocol, but created the uwsgi protocol mentioned above.
The main features of uWSGI are as follows:
Hongmeng official Strategic Cooperation to build HarmonyOS Technology Community
Ultra-fast performance.
Low memory footprint (about half of apache2's mod_wsgi measured).
Multi-app management.
Detailed logging capabilities (which can be used to analyze app performance and bottlenecks).
Highly customizable (memory size limit, restart after a certain number of services, etc.).
Gunicorn:
Tools similar to uWSGi are migrated from rails's deployment tool (Unicorn). But the protocol it uses is the WSGI mentioned above, which is the official standard defined by python2.5 (PEP 333). The root is red and the deployment is relatively simple. For detailed tutorials, please click here (http://gunicorn.org/). Gunicorn uses the prefork mode, and the Gunicorn server is compatible with various Web frameworks, requiring very simple execution, lightweight resource consumption, and quite fast. It is characterized by close integration with Django and convenient deployment. There are also many disadvantages, such as not supporting HTTP 1.1, low concurrent access performance, and a certain performance gap with uWSGI,Gevent and so on.
1. Gunicorn design
Gunicorn is a master process, spawn the web server of several worker processes. The master process controls the emergence and demise of the worker process, which only needs to accept the request and process. This separation makes reload code very convenient and makes it easy to add or decrease worker processes. The author gives a lot of room for expansion, it can support different IO methods, such as Gevent,Sync synchronous process, Asyc asynchronous process, Eventlet and so on. Master is completely separated from the worker process, so that Gunicorn is essentially a service that controls the process.
2. Gunicorn source code structure
Starting with Application.run (), first initialize the configuration, read from the file, read from the terminal, and so on to complete the configurate. Then starting Arbiter,Arbiter is essentially the core of the master process, which is first read and set from the configuration class, and then initializes the signal handling function to establish the socket. Then the spawn worker process is started and spawn is performed according to the number of configured worker processes. Then it enters the polling state, receives the signal, processes the signal, and then continues. The way to wake up the process here is to create a PIPE, write it into pipe through the signal processing function, and then master to wake up from select.select ().
After the spawn, the worker process initializes, then processes the signal as well, and starts polling, processes the HTTP request, calls the application side of WSGI, and gets the resopnse return. And then move on.
The advantage of the Sync synchronization process is that each request is separate and each request failure does not affect other request, but this leads to performance bottlenecks.
Tornado:
Even if Tornado is a python development framework, it is also an asynchronous non-blocking http server. Its own data output implementation does not comply with some of the general protocols mentioned above, because it is a web server, so dynamic requests are output to dynamic content requested by users directly through internal mechanisms. If you use it as a separate server and want to use it to deploy with other frameworks such as Flask, you need to use the WSGI protocol, which is built into Tornado, tornado.wsgi.WSGIContainer.
Wsgiref:
Python comes with wsgi server that implements the WSGI protocol. Wsgi server can be understood as a web server that conforms to the wsgi specification, receives the request request, encapsulates a series of environment variables, and calls the registered wsgi app,*** according to the wsgi specification to return the response to the client. This is Django's own server.
All of the above can be understood as realization! Make it happen! Make it happen! A tool that implements the protocol!
Note: mod_wsgi (the module of apache) is also a module that implements the wsgi protocol, and now it is almost not abandoned, so there is no need to say any more. Check it out for yourself if you are interested.
So if you use the Django framework to develop applications and want to deploy to the production environment, you can definitely not use the Django that comes with it. You can use the uWSGI server that uses the uwsgi protocol, or you can use the gunicorn or Tornado that implements the WSGI protocol, or you can use the Nginx, lighttpd, apache servers of FastCGI or CGI mode. The same is true of other frameworks! When you understand these concepts, you can have a clear idea of them when you deploy them, and you will "know how they are and why" when you match the various tools.
There are two frameworks Django and Tornado in our team's project, and two deployment methods are also used in the production environment. UWSGI and Gunicorn:
The Django project is deployed in Nginx+uWSGI, and the Tornado project is deployed in Nginx+Gunicorn:
Nginx is forwarded as load balancer and static content. The Tornado project uses supervisord to manage Gunicorn and Gunicorn to manage Tornado. As we all know, because of the existence of Python's GIL, the concurrency of Python is in a multi-process mode, so we deploy it in a core of two processes.
Thank you for reading this article carefully. I hope the article "what are the ways to deploy Web in Python" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.