The magical fastcgi_finish_request in fastcgi php

WBOY
Release: 2016-07-29 08:45:01
Original
918 people have browsed it

It may sound a little confusing, let's illustrate it with a few examples:

Copy code The code is as follows:


echo 'Example:';
fastcgi_finish_request();
echo 'To be , or not to be, that is the question.';
file_put_contents('log.txt', 'To be or not to be, that is the question.');
?>


Through the browser (not the command line!) After running this script, it was found that the corresponding string was not output, but the corresponding file was generated. This shows that after calling fastcgi_finish_request, the client response has ended, but at the same time the server script continues to run!
Proper use of this feature can greatly improve the user experience. To strike while the iron is hot, here is another example:

Copy the code The code is as follows:


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 ') . "Convert 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. The browsing is not blocked, but the programs are executed. See the log for details.

Finally, let me remind you, Yahoo mentioned Flush the Buffer Early in Best Practices for Speeding Up Your Web Site, which is to use the flush method in PHP to send the content to the client as soon as possible, although on the surface it is different from this article The introduced fastcgi_finish_request is slightly similar, but essentially completely different, so don’t get confused.
Repost P.S.: I took a look at this method. When called, a response will be sent and the connection will be closed. But it will not end the running of PHP. Compared with calling flush or speeding up your Echo as I introduced before, this method The method can be simpler.
In addition, from the perspective of code portability, you can attach the following code to the code:

Copy the code The code is as follows:


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


will not cause code deployment problems in non-fpm environments.

The above introduces the magical fastcgi_finish_request in fastcgi php, including fastcgi content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!