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 '例子:'; fastcgi_finish_request(); /* 响应完成, 关闭连接 */ /* 记录日志 */file_put_contents('log.txt', '生存还是毁灭,这是个问题.');?>
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 '例子:'; file_put_contents('log.txt', date('Y-m-d H:i:s') . " 上传视频\n", FILE_APPEND); fastcgi_finish_request(); sleep(1);file_put_contents('log.txt', date('Y-m-d H:i:s') . " 转换格式\n", FILE_APPEND); sleep(1);file_put_contents('log.txt', date('Y-m-d H:i:s') . " 提取图片\n", FILE_APPEND); ?>
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() { } }
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:
##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!