How to use Fastcgi_finish_request to improve page response speed

Source: Internet
Author: User
This article mainly introduces the use of fastcgi_finish_request to improve the page response speed, has a certain reference value, now share to everyone, the need for friends can refer to

PHP FPM provides a method called Fastcgi_finish_request when PHP is running in fastcgi mode. As the document says, this method can improve the processing speed of requests, and if some processing can be done after the page has been generated, you can use this method.

It may sound a bit hazy, and we'll illustrate it in a few examples:

<?php Echo ' Example: '; Fastcgi_finish_request (); /* Response complete, close the connection */////* Log */file_put_contents (' Log.txt ', ' Survival or destruction, this is a problem. ');? >

This script is accessed through a browser, and the result is that the corresponding string is not output, but the corresponding file is generated. This means that after calling Fastcgi_finish_request, the client response is over, but the server script continues to run!

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

<?php 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 operations, browsing without being blocked, the program is executed, the specific look at the log.

Finally to give you a wake up, Yahoo in the best practices in speeding up Your Web site mentions the 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 code portability, you can include the following code in the code:

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

Does not cause code deployment to cause problems in non-FPM environments.

The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!

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.