Don't let the server streaking I have learned PHP, the official environment deployment of php is very simple, change a few files to be OK, using FastCgi is also a minute. In contrast, Python's deployment on web applications is cumbersome, mainly due to the variety of tools, and the lack of support for mainstream servers. Before understanding the deployment of Python's production environment, clear some concepts! Very important! CGI: CGI is the Common Gateway Interface, which is the interface standard between external applications (CGI programs) and Web servers. It is a procedure for transferring information between CGI programs and Web servers. The CGI specification allows web servers to execute external programs and send their output to a web browser. CGI turns a set of simple static hypermedia documents from the web into a complete new interactive medium. In general, CGI is like a bridge that connects a web page to an executable program in a web server. It passes the instructions received by the HTML to the server's executable program, and returns the results of the server's execution program to the HTML page. CGI's cross-platform performance is excellent and can be implemented on almost any operating system. When the CGI method encounters a connection request (user request), it first creates a child process of cgi, activates a CGI process, then processes the request, and ends the child process after processing. This is the fork-and-execute mode. So how many cgi child processes there are in the cgi way server, and the sub-process repeated loading is the main reason for the low performance of cgi. When the number of user requests is very large, a large amount of system resources such as memory, CPU time, etc. will be crowded, resulting in inefficiency. CGI script workflow: The browser requests a URL to a CGI application via an HTML form or hyperlink request. The server server sends and receives requests. The specified CGI application. The operations required for a CGI application to perform, usually based on what the viewer entered. The CGI application formats the results into a web server and a document that the browser can understand (usually an HTML page). The web server returns the results to the browser. Python has cgi module to support native cgi programs FastCGI: FastCGI is an interface that communicates between the HTTP server and the dynamic scripting language at a high speed and speed. Most popular HTTP servers support FastCGI, including Apache, Nginx, and lighttpd. At the same time, FastCGI is also supported by many scripting languages, including Python. FastCGI is an improvement from CGI development. The main disadvantage of the traditional CGI interface approach is poor performance, because each time the HTTP server encounters a dynamic program, it needs to restart the script parser to perform the parsing, and the result is returned to the HTTP server. This is almost unavailable when dealing with high concurrent access. FastCGI is like a long-live CGI, it can be executed all the time, as long as it does not take time to fork once every time it is activated (this is CGI's most criticized fork-and-execute mode ). CGI is a so-called short-lived application, and FastCGI is a so-called long-lived application. Because the FastCGI program does not need to constantly generate new processes, it can greatly reduce the pressure on the server and produce high application efficiency. Its speed efficiency is at least 5 times higher than CGI technology. It also supports distributed computing, where FastCGI programs can execute on hosts other than the web server and accept requests from other web servers. FastCGI is a CGI open extension of a language-independent, scalable architecture whose main behavior is to keep the CGI interpreter process in memory and thus achieve higher performance. As we all know, the repeated loading of the CGI interpreter is the main reason for the low 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 the C/S structure, which separates the HTTP server from the script parsing server and starts one or more script parsing daemons on the script parsing server. Each time the HTTP server encounters a dynamic program, it can be delivered directly to the FastCGI process for execution, and the resulting result is returned to the browser. This approach allows the HTTP server to specifically process static requests or return the results of the dynamic script server to the client, which greatly improves the performance of the entire application. FastCGI workflow: Load the FastCGI Process Manager when launching the Web Server (PHP-CGI or PHP-FPM or spawn-cgi) The FastCGI process manager initializes itself, starts multiple CGI interpreter processes (visible with multiple php-cgis) and waits for connections from the Web Server. When a client request arrives at the Web Server, the FastCGI Process Manager selects and connects to a CGI interpreter. The web server sends the CGI environment variables and standard input to the FastCGI child process php-cgi. After the FastCGI child process finishes processing, the standard output and error information are returned from the same connection to the Web Server. 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 the Web Server). In CGI mode, php-cgi exits here. Features of FastCGI: Break the traditional page processing technology. Traditional page processing technology, the program must be on the same server as the Web server or Application server. This history has been broken by FastCGI technology for N years. The application of FastCGI technology can be installed on any server in the server farm, and communicate with the Web server through TCP/IP protocol, which is suitable for developing large-scale distributed. Web groups are also suitable for efficient database control. Clear request mode. CGI technology does not have a clear role. In FastCGI programs, programs are given a clear role (responder role, authenticator role, filter role). WSGI: The Python Web Server Gateway Interface (WSGI) is a simple and versatile 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 a low-level interface between Web servers and Web applications or application frameworks to improve the commonality of portable Web application development. WSGI is designed based on existing CGI standards. WSGI 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. 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 the API, so it can mediate between the WSGI service and the WSGI application: from the perspective of the WSGI server, the middleware plays the application, and from the application point of view, The middleware acts as a server. The Middleware component can perform the following functions: After rewriting the environment variable, the request message is routed to a different application object based on the target URL. Allows multiple applications or application frameworks to run simultaneously in a single process. Load balancing and remoting by forwarding request and response messages over the network. Post-content processing, such as applying XSLT style sheets. Previously, how to choose the right web application framework became a problem that plagued Python beginners because, in general, the choice of web application framework would limit the choice of available web servers, and vice versa. The Python application at the time was usually designed for one of CGI, FastCGI, mod_python, and even for a custom API interface for a particular web server. WSGI has no official implementation, because WSGI is more like a protocol. As long as these protocols are followed, the WSGI application can run on any server, and vice versa. WSGI is Python's CGI wrapper, which is PHP's CGI wrapper compared to Fastcgi. WSGI divides web components into three categories: web server, web middleware, web application, wsgi basic processing mode: WSGI Server -> (WSGI Middleware)* -> WSGI Application. Uwsgi: The uwsgi protocol is a protocol owned by the uWSGI server. It is used to define the type of information. The first 4 bytes of each uwsgi packet is a description of the type of transport information. It is two things compared to WSGI. It is said to be 10 times more efficient than fcgi. The above four can be understood as the agreement! protocol! protocol! By implementing such a protocol, you can implement a web service associated with a web server and a web application! uWSGI: The uWSGI project is designed to develop a complete solution for network applications deploying distributed clusters. uWSGI is primarily targeted at the web and its standard services and has been successfully applied in many different languages. Thanks to uWSGI's extensible architecture, it can be extended to support more platforms and languages ​​with unlimited extensions. Currently, you can write plugins using C, C++ and Objective-C. The "WSGI" in the project name is meant to express thanks to the Python Web standard of the same name, because WSGI developed the first plugin for the project. uWSGI is a web server that implements protocols such as WSGI, uwsgi, and http. uWSGI, instead of the wsgi protocol or the FastCGI protocol, has created the uwsgi protocol mentioned above. The main features of uWSGI are as follows: Ultra-fast performance. Low memory footprint (measured as about half of apache2's mod_wsgi). Multi app management. Detailed logging capabilities (can be used to analyze app performance and bottlenecks). Highly customizable (memory size limit, restart after a certain number of services, etc.). Gunicorn: A tool similar to uWSGi, ported from the rails deployment tool (Unicorn). But the protocol it uses is WSGI as mentioned above. This is the official standard defined in Python 2.5 (PEP 333). The root red seed is positive, and the deployment is relatively simple. Please click here for detailed tutorial (http://gunicorn) .org/). Gunicorn uses the prefork model, and the Gunicorn server is compatible with a variety of web frameworks, requiring very simple execution, lightweight resource consumption, and fairly fast. It is characterized by its close integration with Django and its ease of deployment. There are many disadvantages, HTTP 1.1 is not supported, concurrent access performance is not high, and there is a certain performance gap with uWSGI, Gevent, etc. 1. Gunicorn design Gunicorn is a master process that spawns several web servers for worker processes. The master process controls the generation and demise of the worker process, and the worker process only needs to accept the request and process it. This separation makes the reload code very convenient and makes it easy to increase or decrease the work process. The author of the worker process gives a lot of room for extension, it can support different IO methods, such as Gevent, Sync synchronization process, Asyc asynchronous process, Eventlet and so on. The master is completely separate from the worker process, making Gunicorn essentially a service that controls the process. 2. Gunicorn source structure Starting with Application.run(), first initialize the configuration, read from the file, read from the terminal, etc. to complete the configurate. Then start Arbiter, which is the core of the essentially master process. It first reads and sets it from the configuration class, then initializes the signal handler and creates the socket. Then start the spawn worker process and spawn based on 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 to the pipe through the signal handler, and then the master wakes up from select.select(). After the worker process is spawned, it starts to initialize, then processes the signal as well, starts polling, processes the HTTP request, calls the WSGI application, and returns resopnse. Then continue. The advantage of the Sync synchronization process is that each request is detached, and each request failure does not affect other requests, but this leads to a performance bottleneck. Tornado: Even a Python development framework, Tornado, is an asynchronous non-blocking http server. Its data output implementation does not follow some of the common protocols mentioned above. Because it is a web server, dynamic requests are directly passed through the internal The mechanism outputs the dynamic content requested by the user. 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, Tornado built the protocol, tornado.wsgi.WSGIContainer. Wsgiref: Python comes with a wsgi server that implements the WSGI protocol. The wsgi server can be understood as a web server conforming to the wsgi specification, receiving request requests, encapsulating a series of environment variables, calling the registered wsgi app according to the wsgi specification, and finally returning the response to the client. Django's own server is it. All of the above can be understood as realization! achieve! achieve! A tool that implements the protocol! Note: mod_wsgi (apache module) is actually a module that implements the wsgi protocol, and it is almost never discarded, so I don't have much to say, so check it out. So if you develop the application using the Django framework and want to deploy to the production environment, you can't use Django. You can use the uWSGI server using the uwsgi protocol, the gunicorn or Tornado that implements the WSGI protocol, or the FastCGI. Nginx, lighttpd, apache server in CGI mode. The same is true of other frameworks! Understand that these concepts can be done at the time of deployment, and the combination of tools will be "knowing it and knowing why." There are two frameworks for Django and Tornado in our group of projects, and the production environment also uses two deployment methods. uWSGI and Gunicorn: The Django project is deployed in Nginx+uWSGI mode, and the Tornado project is deployed in Nginx+Gunicorn mode: Nginx acts as both load balancing and static content forwarding. 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, Python's concurrency uses a multi-process mode, so the way we deploy is a core two processes. Oil Filter Assembly,Washer for Engine Generator,Oil Filter ,Engine Oil Filter Spare PartsParts Jinan Guohua Green Power Equipment Co.,Ltd. , https://www.guohuagenerator.com