Home > php教程 > php手册 > body text

php中神奇的fastcgi_finish_request

WBOY
Release: 2016-06-13 12:10:19
Original
1465 people have browsed it

听起来可能有些茫然,我们通过几个例子来说明一下:

复制代码 代码如下:


echo '例子:';
fastcgi_finish_request();
echo 'To be, or not to be, that is the question.';
file_put_contents('log.txt', '生存还是毁灭,这是个问题。');
?>


通过浏览器(不是命令行!)运行此脚本,结果发现并没有输出相应的字符串,但却生成了相应的文件。由此说明在调用fastcgi_finish_request后,客户端响应就已经结束,但与此同时服务端脚本却继续运行!
合理利用这个特性可以大大提升用户体验,趁热打铁再来一个例子:

复制代码 代码如下:


echo '例子:';
file_put_contents('log.txt', date('Y-m-d H:i:s') . " 上传视频\n", FILE_APPEND);
fastcgi_finish_request();
sleep(1);
file_put_contents('log.txt', date('Y-m-d H:i:s') . " 转换格式\n", FILE_APPEND);
sleep(1);
file_put_contents('log.txt', date('Y-m-d H:i:s') . " 提取图片\n", FILE_APPEND);
?>


代码里用sleep模拟一些耗时的操作,浏览时没有被堵塞,程序却都执行了,具体看日志。

末了给您提个醒,Yahoo在Best Practices for Speeding Up Your Web Site中提到了Flush the Buffer Early,也就是利用PHP中的flush方法把内容尽快发到客户端去,虽然表面上它和本文介绍的fastcgi_finish_request有些许的类似,但本质上完全不同,别混淆了。

转载附言: 我看了下这个方法, 在调用的时候, 会发送响应, 关闭连接. 但是不会结束PHP的运行. 相比调用flush, 或者我之前介绍的加速你的Echo来说, 这个方法能更加干脆一些.

另外, 从代码的可移植性讲的话, 可以在代码中附上如下代码:

复制代码 代码如下:


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


不会造成代码部署在非fpm环境下造成问题.
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 Recommendations
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!