执行脚本时如何获取部分输出? [英] How to get partial output when script is executing?

查看:112
本文介绍了执行脚本时如何获取部分输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个脚本,可以在更长的时间内执行多项操作.在标准情况下,PHP脚本的输出在完全执行此脚本后发送.如果我的脚本加载时间很长而没有任何响应,用户可能会感到失望.

I have a script which do multiple actions in longer time. In standard situation output from PHP script is sent after full execution of this script. User may be dissapointed if my script will load very long time without any response.

比方说,我在此脚本中有一个循环,该循环将工作10次. 每次执行完一次后,我都希望输出到浏览器"Element X finished. Y time.". X和Y的值将改变.

Let's say I have in this script a loop which will be working 10 times. After each one execution I want to get output to browser "Element X finished. Y time.". Value of X and Y will be changing.

有什么办法可以达到这种效果?也许从该脚本到新脚本的某种调用将通过AJAX将数据发送到浏览器.

Is there any way to reach that effect? Maybe with some kind of call from this script to new script which will sent data to browser throug AJAX.

还是只有通过PHP实现才能达到这种效果?

Or is there any way to reach this effect only with PHP implementation?

我看到我必须澄清这个问题.我希望此信息动态显示在页面上.我已经尝试过使用输出缓冲区功能,但是仍然无法使用,因为我需要它.完全执行脚本后,将显示浏览器中的输出.测试代码:

I see that I have to clarify the matter. I want this information to appear dynamically on the page. I tried with output buffer functions already, and it still don't work as I need it. Output in browser is showing after full execute of script. Test code:

for ($i = 0; $i <= 10; $i++) {
    ob_start();

    echo "Output " . $i . "<br />";

    ob_flush(); //also only flush() etc.


    sleep(1);
}

推荐答案

是的,仅使用PHP即可实现.我记得当我在一段时间内通过wordpress进行跟踪,更新模块时,必须进行ftp,解压缩,复制文件等操作,这需要很长时间,并且喜欢保持用户的最新状态,因此我看到了与此相关的一些代码.在检查他们的send_message()函数并阅读PHP ob_flush之后,当前正在做什么()页面,我认为您想要:

Yes, it should be achieveable with PHP alone. I remember seeing some code related to this when I was tracing through wordpress a while back, when it updates modules, it has to ftp, unzip, copy files, etc, which take a long time, and it likes to keep the user updated with what it's doing at the moment... after inspecting their send_message() function, and reading the PHP ob_flush() page, i think you want:

echo "stuff\n"; // adding a newline may be important here,
                // a lot of io routines use some variant of get_line()
ob_end_flush(); // to get php's internal buffers out into the operating system
flush(); // to tell the operating system to flush it's buffers to the user.

php手册上的人还暗示,它可能有助于显式地设置带有mime类型和字符集的header(),因此浏览器会在开始时就知道这一点,而不必等待整个过程.在尝试解密其具有的实体之前,二进制对象变得可用.

The people at the php manual also implied that it might help to explicitly set a header() with the mime-type and character set in it, so the browser would know that at the start and wouldn't wait for the entire binary object to become available before attempting to decypher what sort of entity it had.

如果这不起作用,则需要进一步修改系统php.ini以关闭输出缓冲和压缩,这时您最好查看ajax解决方案.

If that doesn't work you'd need to further modify your system php.ini to turn output buffering and compression off, at which point you may as well go look at an ajax solution.

ajax解决方案如下:

An ajax solution would look something like:

您的脚本吐出一些html/javascript来触发ajax请求,刷新其所有缓冲区,并停止与用户有关,然后开始某些操作,或者仅表明某些大操作将在某个地方的数据库中开始可以完成一份计划工作.您页面上的javascript会有一个计时器循环,以轮询ajax端点的状态,直到ajax答复它已完成. Ajax端点将通过查询数据库或检查输出文件等来检查任务的状态,并立即吐出所有已知信息并终止,让客户端决定何时再次询问是否再次完成任务.这项工作涉及更多的复杂性,需要更多的活动部件,但是如果值得您花时间去做,那对于用户来说则是一个非常不错的最终产品.

Your script spits out some html/javascript to hit an ajax request, flushes all it's buffers, and stops concerning itself with the user, then starts some operation, or perhaps merely indicates that some large operation is to commence in a database somewhere for a cron job to pick up. The javascript on your page would have a timer loop to poll an ajax endpoint for status until the ajax replied that it was complete. The ajax endpoint would check on the status of your task by querying the database, or examining output files, etc, and spit all it knew out at once and terminate, letting the client decide when to ask it if things were done again. It is a much more involved and complicated endevour with more moving parts, but achieves a very nice end product for the user, if it's worth your time to craft.

这篇关于执行脚本时如何获取部分输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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