How to use fastcgi_finish_request to improve page response speed

不言
Release: 2023-04-02 11:32:02
Original
1684 people have browsed it

This article mainly introduces the use of fastcgi_finish_request to improve page response speed. It has certain reference value. Now I share it with everyone. Friends in need can refer to it.

When PHP is running in FastCGI mode, PHP FPM provides a method called fastcgi_finish_request. According to the documentation, this method can improve the processing speed of requests. If some processing can be performed after the page is generated, you can use this method.

Listen It may sound a little confusing. Let’s explain it through a few examples:

<?php 
echo &#39;例子:&#39;;
fastcgi_finish_request(); /* 响应完成, 关闭连接 */
 /* 记录日志 */file_put_contents(&#39;log.txt&#39;, &#39;生存还是毁灭,这是个问题.&#39;);?>
Copy after login

Access this script through the browser, and it turns out that the corresponding string is not output. But the corresponding file was generated. This means that after calling fastcgi_finish_request, the client response has ended, but at the same time the server script continues to run!

Proper use of this feature can greatly improve the user experience. Strike while the iron is hot and give another example:

<?php 
echo &#39;例子:&#39;; 
file_put_contents(&#39;log.txt&#39;, date(&#39;Y-m-d H:i:s&#39;) . " 上传视频\n", FILE_APPEND);
 
fastcgi_finish_request(); 
sleep(1);file_put_contents(&#39;log.txt&#39;, date(&#39;Y-m-d H:i:s&#39;) . " 转换格式\n", FILE_APPEND); 
sleep(1);file_put_contents(&#39;log.txt&#39;, date(&#39;Y-m-d H:i:s&#39;) . " 提取图片\n", FILE_APPEND); 
?>
Copy after login

Use sleep to simulate some time-consuming operations in the code , the browsing was not blocked, but all the programs were executed. Please see the log for details.

Finally, let me remind you that Yahoo mentioned Flush the Buffer Early in Best Practices for Speeding Up Your Web Site, that is, Use the flush method in PHP to send the content to the client as soon as possible. It is slightly similar to the fastcgi_finish_request introduced in this article.

In addition, from the perspective of code portability, you can attach the following to the code Code:

if (!function_exists("fastcgi_finish_request")) {      
function fastcgi_finish_request()  {
      }
}
Copy after login

will not cause code deployment problems in non-fpm environments.

The above is the entire content of this article, I hope it will be helpful to everyone Learning will be helpful. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

How to use instanceof

##Introduction to the extension function of the memcache class

The above is the detailed content of How to use fastcgi_finish_request to improve page response speed. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!