Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use fastcgi_finish_request of PHP

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to use PHP's fastcgi_finish_request". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use PHP's fastcgi_finish_request.

PHP operation mode

CGI Universal Gateway Interface / Common Gateway Interface

CGI is an older model that has been rarely used in recent years.

Introduction: every user request will first create a child process of CGI, then process the request, and then end the child process, which is the Fork-And-Execute pattern.

When the number of user requests is very large, it will occupy a lot of system resources such as memory, CPU time and so on.

Disadvantages: in the case of high access requirements, the CGI process Fork will become a heavy burden on the server.

FastCGI (resident CGI / Long-Live CGI)

It's used a lot.

Introduction: FastCGI is an upgraded version of CGI. FastCGI is like a resident (long-live) CGI.

It can be executed all the time, as long as it is activated, it does not take time to Fork every time.

FastCGI is a scalable, high-speed communication interface between HTTP server and dynamic scripting languages.

Popular HTTP server, such as Apache, Nginx, lighttpd, all support FastCGI.

Principle:

Load the FastCGI process Manager (PHP-FPM) when 1.Web Server starts

The 2.FastCGI process manager initializes and starts multiple CGI interpreter processes and waits for a connection from Web Server

3. When the client request reaches Web Server, the FastCGI process manager selects and connects to a CGI interpreter

4.Web server sends CGI environment variables and standard input to FastCGI child process php-cgi

5. After completing the processing, the FastCGI child process returns the standard output and error information from the same connection to 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.

CLI mode

Introduction: PHP-CLI is the abbreviation of PHP Command Line Interface, which means PHP runs on the command line.

PHP-CLI mode is supported under Windows and Linux.

Module mode

Introduction: module mode is integrated in the form of mod_php5 module.

The function of the mod_php5 module is to receive the PHP file requests passed by Apache, process these requests, and then return the processed results to Apache.

Add a line to the configuration file httpd.conf of Apache:

1LoadModule php5_module modules/mod_php5.so

The above is just a simple way to let you know, if you are interested, you can consult the relevant articles.

Next, let's introduce: fastcgi_finish_request.

Fastcgi_finish_request

When PHP runs in FastCGI mode, FPM provides a method: fastcgi_finish_request.

Official address: http://php.net/manual/zh/function.fastcgi-finish-request.php

Official explanation: flush all response data to the client.

Personal understanding: when a method is called, a response is sent and the connection is closed, but it does not end the PHP.

If you don't understand, you can run the following code directly:

12345678910111213141516 echo date. "\ r\ n"; / will output fastcgi_finish_request (); set_time_limit (0); / / avoid timeout error ini_set ('memory_limit','-1'); / / avoid out-of-memory sleep (5); $time = date ('Y-m-d Hlav itime slots, time ()). "\ r\ n"; echo $time / / file_put_contents ('test.txt', $time, FILE_APPEND) will not be output

After executing this function, you will find that you can implement asynchronous operations and improve the response speed.

You can use the fastcgi_finish_request () function to integrate the queue and send messages to the queue asynchronously.

Because this function exists only in FastCGI mode, you can add the following code for portability:

1234if (! function_exists ("fastcgi_finish_request")) {function fastcgi_finish_request () {}} so far, I believe you have a better understanding of "how to use PHP's fastcgi_finish_request". You might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue 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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report