0

我有一个 php 脚本(在普通的 LAMP 环境中),它在脚本末尾运行几个家务任务。

我使用 flush() 将所有数据推送到客户端,效果很好(页面已完全加载),但浏览器仍在等待数据(由“加载”动画指示),这让用户感到困惑,但当然很清楚,因为 Apache 无法知道 PHP 是否会在 flush() 之后生成更多输出 - 但是在我的情况下它永远不会。

有没有办法告诉客户端输出已经完成并且即使脚本继续运行也应该立即关闭 http 连接?

4

2 回答 2

0

It sounds like you have a long running script performing varioous tasks. Especially it appears to script goes on doing things after it has sent the reply to the client. This is a design that opens a whole lot of potential problems. You should re-think your architecture.

Keep house keeping tasks and client communication strictly separate. For example you could have a client request processed and trigger internal sub requests (which you can detach from) or deligate tasks to a cron like system. Then offer a second view to the client which visualized the progress and result of those tasks. This approach is much safer, more flexible and easier to extend when required. And your problem at hand is solved, too :-)

于 2013-04-15T05:20:08.533 回答
0

您可以使用此功能fastcgi_finish_request()特殊功能来完成请求并刷新所有数据,同时继续做一些耗时的事情(视频转换,统计处理等);http://php.net/manual/en/install.fpm.php但你需要为它安装 FPM

<?php
 echo "You can see this from the browser immediately.<br>";
 fastcgi_finish_request();
 sleep(10);
 echo "You can't see this form the browser.";
?>
于 2013-04-15T05:39:30.520 回答