有没有办法让PHP随着脚本的执行逐步输出?

发布于 2024-08-27 05:23:07 字数 259 浏览 6 评论 0原文

因此,我正在编写一个供我个人使用的一次性脚本,我希望能够了解该过程的进展情况。基本上我正在处理几千个媒体发布并将它们发送到我们的新 CMS。

因此,我不会敲击 CMS,而是在每 5 个请求后让脚本休眠几秒钟。

我希望 - 在脚本执行时 - 能够看到我的 echo 告诉我脚本将要休眠或者与 Web 服务的最后一个事务已成功。

这在 PHP 中可能吗?

感谢您的帮助!

伊恩

So I'm writing a disposable script for my own personal single use and I want to be able see how the process is going. Basically I'm processing a couple of thousand media releases and sending them to our new CMS.

So I don't hammer the CMS, I'm making the script sleep for a couple of seconds after every 5 requests.

I would like - as the script is executing - to be able to see my echos telling me the script is going to sleep or that the last transaction with the webservice was successful.

Is this possible in PHP?

Thanks for your help!

Iain

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

污味仙女 2024-09-03 05:23:08

您可能想探索刷新命令 http://php.net/manual/en/ function.flush.php

You may want to explore the flush commands http://php.net/manual/en/function.flush.php

秋日私语 2024-09-03 05:23:07

使用ob_flush发送缓冲区中的任何数据。因此,您可以执行一些命令,刷新输出,然后在处理更多命令之前休眠一会儿。

我确实注意到,在特别长的脚本上,PHP 似乎开始发送缓冲区中的内容,即使脚本尚未完成执行

默认情况下,PHP 永远不会回显任何内容,直到整个脚本完成执行。这只是您的浏览器试图一次输出太多数据,或者您有某些东西导致它在某处刷新。

Use ob_flush to send any data in the buffer. So you can execute some commands, flush the output, and then sleep for a bit before processing more commands.

I do notice that on particularly long scripts, PHP seems to start sending what it's got in the buffer even though the script hasn't finished executing

By default, PHP will never echo anything until the entire script finishes executing. That's just your browser trying to output way too much data at once, or you have something that's causing it to flush somewhere.

一袭水袖舞倾城 2024-09-03 05:23:07

这是一个将其包装起来的 PHP 包:

https://packagist.org/packages/coderofsalvation/browser -流

   BrowserStream::enable();
    BrowserStream::put("loading");

    for( $i = 0; $i < 10; $i++ ){
        BrowserStream::put(".");
        sleep(1);
    }

Here's a PHP package which wraps it up :

https://packagist.org/packages/coderofsalvation/browser-stream

   BrowserStream::enable();
    BrowserStream::put("loading");

    for( $i = 0; $i < 10; $i++ ){
        BrowserStream::put(".");
        sleep(1);
    }
饭团 2024-09-03 05:23:07

您可以尝试使用 flush 并查看其他 输出控制函数,但它们可能没有任何用处。不管怎样,您的网络服务器软件可能会缓冲响应。

You can try using flush and look at the other output control functions, but they might not be any use. Your web server software may buffer the response regardless.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文