Use fastcgi_finish_request to implement background asynchronous processing and improve page response speed

Source: Internet
Author: User
Fastcgi_finish_request () is to flush all the response data to the client and end the request. This allows the client to continue running tasks that require a large amount of time after the client ends the connection, when PHP runs in FastCGI mode... fastcgi_finish_request () is to flush all the response data to the client and end the request. This allows the client to continue running tasks that require a large amount of time after the client ends the connection, when PHP runs in FastCGI mode, php fpm provides a method named fastcgi_finish_request. according to the document, this method can increase the request processing speed. if some processing can be performed after the page is generated, you can use this method.

It may sound a little confused. let's use several examples to illustrate:

 

When you access this script through a browser, it is found that no corresponding string is output, but the corresponding file is generated. it indicates that after fastcgi_finish_request is called, the client response has ended, but the server script continues to run at the same time!

The rational use of this feature can greatly improve user experience. let's take another example:

  

The code uses sleep to simulate some time-consuming operations. when browsing, the program is not blocked, but all the operations are executed. for details, see the log.

Finally, I will remind you that Yahoo mentioned Flush the Buffer Early in Best Practices for Speeding Up Your Web Site, that is, using the flush method in PHP to send the content to the client as soon as possible, it is somewhat similar to the fastcgi_finish_request described in this article.

In addition, from the perspective of code portability, you can include the following code in the code:

if (!function_exists("fastcgi_finish_request")) {  function fastcgi_finish_request() {  } }

This will not cause problems when the code is deployed in a non-fpm environment.


Article URL:

Reprint ^ at will, but please attach the tutorial address.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.