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.