The magical fastcgi_finish_request_PHP tutorial in php

WBOY
Release: 2016-07-21 15:30:30
Original
839 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', 'Survival Or destroy, that's the question. ');
?>

Run this script through the browser (not the command line!) and found that the corresponding string is not output, but The corresponding files are 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. 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);
?>

Sleep is used in the code 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 the same as The fastcgi_finish_request introduced in this article is slightly similar, but it is completely different in nature, so don’t get confused.

Reprint PS: 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 what I introduced before, it will speed up your For Echo, this method is simpler.

In addition, in terms of code portability, you can attach the following code to the code:

Copy Code The code is as follows:

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

Won’t cause code deployment problems in non-fpm environments.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/323231.htmlTechArticleIt may sound a little confusing. Let’s illustrate it through a few examples: Copy the code as follows: ?php echo ' Example: '; fastcgi_finish_request(); echo 'To be, or not to be, that...
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!