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!