


How to use fastcgi_finish_request to improve page response speed
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Alipay PHP...

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.
