Using Fastcgi_finish_request to implement background asynchronous processing and improve page response speed

Source: Internet
Author: User

PHP FPM provides a method named Fastcgi_finish_request when PHP is running in fastcgi mode. According to the documentation, this method can increase the processing speed of the request, which can be used if some processing can be done after the page has been generated.

It may sound a little dazed, and we'll illustrate this by a few examples:

The code is as follows Copy Code
<?php

Echo ' Example: ';
Fastcgi_finish_request (); /* Response complete, close the connection * *

/* Logging Log/*
File_put_contents (' log.txt ', ' Survival or destruction, this is a problem. ');
?>

The script was accessed through the browser, and the result found that the corresponding string was not output, but the corresponding file was generated. This indicates that the client response has ended after the call to Fastcgi_finish_request, but the server-side script continues to run!

Reasonable use of this feature can greatly enhance the user experience, the hot strike Another example:

  code is as follows copy code
<?php
&NBSP
Echo ' Example: ';
 
file_put_contents (' Log.txt ', date (' y-m-d h:i:s '). "Upload video n", File_append);
 
Fastcgi_finish_request ();
 
Sleep (1);
file_put_contents (' Log.txt ', date (' y-m-d h:i: S '). "Conversion format n", File_append);
 
Sleep (1);
file_put_contents (' Log.txt ', date (' y-m-d h:i:s '). "Extract picture n", file_append);
 


The code uses sleep to simulate some time-consuming operation, browsing is not blocked, the program is executed, the specific look at the log.

Finally give you a wake-up call, Yahoo in best practices for speeding up Your Web site mentioned flush the Buffer Early, that is, using the Flush method in PHP to send 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 portability of the code, you can enclose the following code in your code:

The code is as follows Copy Code
if (!function_exists ("Fastcgi_finish_request")) {
function Fastcgi_finish_request () {
}
}



Does not cause code deployment to cause problems in a FPM environment.

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.