1

I wasn't sure how to title this thread, sorry.

I have a script that processes some logs and I echo a lot of debug information as the process goes. Since moving to the new server, it seems that the script hangs for 30 odd seconds, then spits out all the logging, then hangs again for 30 odd seconds and the process continues.

This is really odd behavior and I don't know where to start. Its like it isn't processing the file line by line but in blocks ...

PHP version is 5.1.6 on a CentOS running plesk. (My old CP was CPanel)

Any ideas?

EDIT: Simple example of my issue - Running this code:

for ($i=0; $i<100; $i++) {
echo "test $i";
sleep(1);
}

the script will hang for 100 seconds, then print out all the "test 1" ect. Sleep is required in my main script and on the other server just echoed the values in turn.

EDIT2: Have tried setting output_buffering = 0 and implicit_flush = On and didn't help.

webnoob
  • 15,747
  • 13
  • 83
  • 165
  • Start by turning on `display_errors` and then posting some code, particularly where it appears to hault, around 20 lines around that. We cannot do much with just the server information. – Jim Jun 05 '11 at 14:02
  • My code is to large to post so I am trying to create a simple example. – webnoob Jun 05 '11 at 14:09

1 Answers1

4

You may have output_buffering On. Try to disable it first.

You can do it either in the php.ini file, in a .htaccess file if your server allows it, or use the following code at the beginning of your PHP script:

while (ob_get_level()) ob_end_clean();

Also, use flush() after each echo or print, and it should be all right!

Update: You might also encounter other buffers that you cannot control from within PHP (web server, browser, ...), which is why you're still not seing anything. A workaround is to send some blank bytes after each print:

while (ob_get_level()) ob_end_clean();

for ($i=0; $i<100; $i++) {
    echo "test $i";
    echo str_repeat(' ', 256);
    flush();
    sleep(1);
}

However, while this example works for me on IE & Firefox, it does not work on Chrome!

BenMorel
  • 34,448
  • 50
  • 182
  • 322
  • I have tried this suggestion with my for loop above and it doesn't help :( – webnoob Jun 05 '11 at 14:14
  • 1
    Maybe apache (or a load-balancer) is buffering the output. – Bob Fanger Jun 05 '11 at 14:26
  • Well the exact same code works in Firefox on my other server so I guess this has to be a setup issue. My other machine is a VPS whereas this is a dedibox. Going to try your code now. – webnoob Jun 05 '11 at 14:37
  • Worked! Ok ... Is this something that could be configured via the php.ini ? I Set the output_buffering to 0 and that didn't help .. How come this works? – webnoob Jun 05 '11 at 14:38
  • I think it just won't make any difference, as `output_buffering` just acts as if you had a manual `ob_start()` call. I think it might be an apache buffer issue, maybe the link provided by [this question](http://stackoverflow.com/questions/2072219/how-output-buffering-blocks-in-php-apache-works) will give you a clue. – BenMorel Jun 05 '11 at 14:45