Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Forcing output before script is done. 1

Status
Not open for further replies.

Dustman

Programmer
May 7, 2001
320
US
I've always had problems with this and never seemed to come across an answer. I use php a lot to parse huge files into our databases. Sometimes the script will have to run up to 15 minutes. I usually put print statments inside the loops to give an update of where the script is but sometimes it takes them forever to actually be sent to the client.

I've tried output buffering and sending ob_flush() after every print statement but this still doesn't do it. Even the print statement at the begining of the script doesn't show up for a while.. sometimes its 30 seconds.. sometimes its 2 minutes.. sometimes it waits until the script has finished. Am I missing a buffer somewhere?

-Dustin
Rom 8:28
 
Some PHP installations are configured to have output-buffering turned on by default. What's the value of "output_buffering" in your php.ini?


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Its set to Off. I tried manually turning it on but had no luck.

Code:
function startMyProcess($data) {
  foreach($data as $cnt =>$df) {
    print "Processing for ".$df->id."<br>";
    ob_flush();
    //blah blah blah
  }
}

ob_start();
print "Starting process...<br>";
startMyProcess($myData);
print "Finished process."
ob_end_flush();

You would think code like this would immediatly print "Starting process..." then continue on but it does not. Sometimes it takes a long time and then it will print that and several of the other print statements.

-Dustin
Rom 8:28
 
Your web server is to blame. Probably 'chunked' transfer is turned on. That means you have to flood the output buffer with something to get it pushed to the client.
Use a str_repeat(' ',8192) in a print statement and it will work.
 
That code should specifically NOT immediately show "Starting process....". You should not see "Starting process..." until the flush.


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Ahh.. DRJ478 nailed it. I force more output and it show immediately. Now heres the question, can that be disabled via htaccess? If I understand correctly, its actually needed for dynamic pages to be used with persistant connections. Obviously I don't want to do much to my config to mess up the normal operation. I can just output space like that in my scripts.

And sorry about the slip up, i didn't mean immediately show after the print statement.. i meant immediate after the flush statement (because its immediately after the print at the top of the script.)

Thanks for the help.

-Dustin
Rom 8:28
 
needed for dynamic pages to be used with persistant connections
What do you mean?
With PHP there's really nothing that is persistent. Even db connections opened with pconnect() are only persistent in terms of the Web server child process that hit the specific connection. But 'persitent', no such thing.
I would not mess with the web server config. Pushing out the extra spaces into the transfer buffer to send the chunk should work.
 
I was just going off of what little research I read online. Not the same as persistant connections with php. This has to do with the client browser (or whatever may be connecting) sending the "transfer-Encoding: chunked" header. Without the chunked setting, the server is supposed to send the filesize before it can write the rest of the page headers and the page. They came up with this chunked mode so it can send chunks at a time.

Thats my understanding of it. And yes, the extra spaces are fine for what I need right now. There have a been a few times in the past that it would be a pain because of the extra spaces would be in the way for some log parsing processes. Thanks for the help!

-Dustin
Rom 8:28
 
Code:
ob_start();
print "Starting process...<br>";
startMyProcess($myData);
print "Finished process."
ob_end_flush();

Why would that print "Starting process..." straight away? If you did an ob_flush() after the print then maybe, but you're not flushing it until it has finished everything there.
 
Oops.. sorry. I forgot to add that in my sample code. There is supposed to be a ob_flush() after that print statement. I just typed the example on the fly.. its not my actual code. My code has it after every print statement (now with a str_repeat(' ',8192) after them too). The final working code for my sample would be:

Code:
function startMyProcess($data) {
  foreach($data as $cnt =>$df) {
    print "Processing for ".$df->id."<br>";
    print str_repeat(' ',8192);
    ob_flush();
    //blah blah blah
  }
}

ob_start();
print "Starting process...<br>";
print str_repeat(' ',8192);
ob_flush();
startMyProcess($myData);
print "Finished process."
ob_end_flush();

Again.. thanks for the help eveybody.

-Dustin
Rom 8:28
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top