如何将数据刷新到浏览器但继续执行 [英] How to flush data to browser but continue executing

查看:95
本文介绍了如何将数据刷新到浏览器但继续执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ob_start()和一个相应的ob_flush().我想刷新一部分数据并继续执行其余部分.使用ob_flush()并没有帮助.另外,如果可能需要休息,而无需在浏览器中显示加载.

我不想使用ajax

解决方案

ob_flush写入缓冲区.换句话说,ob_flush告诉PHP给Apache(或nginx/lighttpd/任何)输出,然后让PHP忘记它. Apache获得输出后,就可以使用它执行任何操作. (换句话说,在ob_flush之后,是否立即将其写入浏览器已不在您的控制范围之内.)

因此,简短的答案:没有保证的方法.

仅凭猜测,您可能正在寻找AJAX.每当人们尝试在加载页面内容时进行操作时,AJAX几乎总是正确的路径.

如果您想在后台继续执行任务,可以使用ignore_user_abort,详细信息请参见此处

我会尝试将其从面对网络的内容中提取出来.这可能意味着cron条目或只是从PHP内部产生了一个后台进程(尽管该进程是从脚本执行内部启动的,但不会随脚本一起消失,并且脚本将不等待其完成才死掉).

如果您确实选择了这条路线,则意味着您必要时甚至可以建立某种状态系统.然后,您可以监视执行情况并为用户提供进度的定期更新. (从技术上讲,您也可以使用ignore_user_abort -ed脚本来创建状态系统,但对我来说,这似乎并不干净.)

I have a ob_start() and a corresponding ob_flush(). I would like to flush a portion of data and continue executing the rest. Using ob_flush() didn't help. Also if possible rest needs to happen without showing loading in browser.

EDIT:

I don't want to use ajax

解决方案

ob_flush writes the buffer. In other words, ob_flush tells PHP to give Apache (or nginx/lighttpd/whatever) the output and then for PHP to forget about it. Once Apache has the output, it does whatever it wants with it. (In other words, after ob_flush it's out of your control whether or not it gets immediately written to the browser).

So, short answer: There's no guaranteed way to do that.

Just a guess, you're likely looking for AJAX. Whenever people are trying to manipulate when page content loads as you're doing, AJAX is almost always the correct path.

If you want to continue a task in the background, you can use ignore_user_abort, as detailed here, however, that is often not the optimal approach. You essentially lose control over that thread, and in my opinion, a web server thread is not where heavy processing belongs.

I would try to extract it out of the web facing stuff. This could mean a cron entry or just spawning a background process from inside of PHP (a process that though started from inside of script execution will not die with the script, and the script will not wait for it to finish before dying).

If you do go that route, it will mean that you can even make some kind of status system if necessary. Then you could monitor the execution and give the user periodic updates on the progress. (Technically you could make a status system with a ignore_user_abort-ed script too, but it doesn't seem as clean to me.)

这篇关于如何将数据刷新到浏览器但继续执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆