php output with sleep()

前端 未结 3 421
南方客
南方客 2020-12-01 19:36

I\'m trying to run a loop every second for 25 seconds basically.

for($i = 0; $i <= 25; $i += 1){ 
    echo $i;
    sleep(1)
}

The thin

相关标签:
3条回答
  • 2020-12-01 20:16

    I just hashed through this same problem from a beginner perspective and came up with this bare-bones script which will do what you want.

    <?PHP
    ob_start();
    $buffer = str_repeat(" ", 4096)."\r\n<span></span>\r\n";
    
    for ($i=0; $i<25; $i++) {
      echo $buffer.$i;
      ob_flush();
      flush();
      sleep(1);
    }
    
    ob_end_flush();
    ?>
    

    Questions that you may ask could be here (about \r\n) and here (about ob_flush()). Hope that helps you out.

    0 讨论(0)
  • 2020-12-01 20:24

    Call flush will force PHP to push all of the output buffer to the client before proceeding.

    for($i = 0; $i <= 25; $i += 1){ 
        echo $i;
        flush();
        sleep(1);
    }
    

    EDIT:

    After testing this on my lighttpd server I noticed that it buffered my outputs in blocks of 4096 characters, and I assume other browser might have similar buffering schemes. Also GZIP can prevent flush completely. Unfortunately there is no way to test that it's working due to the nature of HTTP.

    Also another issue with this strategy is that it leaves that PHP proc blocked to other requests. This can cause requests to pile up.

    0 讨论(0)
  • 2020-12-01 20:33

    What you're trying to achieve is incremental output to the browser from PHP.

    Whether this is achievable can depend on your server and how you're invoking PHP.

    PHP under FastCGI

    You're probably a bit more likely to run into this kind of problem when PHP is running under FastCGI rather than as an Apache module, because the coupling between the server and the PHP processes is not as tightly coupled. FastCGI communication uses output buffering once the data has left the PHP processes, with the output sent to the browser only once the request is fully complete, or this buffer has filled up. On top of this, the PHP processes tend to be terminated after a certain amount of time, to avoid letting any one run for too long.

    That said, a combination of ob_end_flush() (or ob_flush()) and flush() should still cause PHP to request that the downstream buffers are cleared, so this may still work. You may need to also investigate whether you need to length the time limit for PHP scripts.

    PHP under mod_php

    If you're using mod_php, you can write incrementally out to the browser. Use the flush() command to ensure that the PHP module will flush it instantly. If you don't have output buffering, or some Apache module such as mod_gzip, then it should go out instantly to the user's browser. What's more, you can keep your PHP script running as long as you like (with set_time_limit() in PHP), under the default configurations, though of course it will consume some memory.

    You may run into trouble with some browsers which don't start rendering the page until a certain amount of a page is downloaded. Some versions of IE may wait for 1KB. I've found that Chrome can wait for more. A lot of people get around this by adding padding, such as a long comment 1 or 2 KB long at the top of the document.

    0 讨论(0)
提交回复
热议问题