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:
<? PHP echo 'example: '; fastcgi_finish_request ();/* response complete, close connection * // * record log */file_put_contents('log.txt', 'survival or destruction, which is a problem. ');?>
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:
<? 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 image \ n", file_append) ;?>
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.
Reprinted postscript: After reading this method, a response will be sent during the call to close the connection. but it will not end the PHP operation. this method is simpler than calling flush or accelerating your echo.
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() { } }