ob_flush takes long time to be executed
Asked Answered
C

4

10

In my website(running with drupal) the ob_flush function takes a long time(between 10 - 100 secs) to be executed. How do I find out why? What can cause this so long time? enter image description here

Coeternal answered 16/12, 2012 at 17:56 Comment(7)
What makes you think this function freezes up your application?Carnage
@Carnage Please see the image I just uploadedCoeternal
Can you please also paste code with ob_start() and ob_flush() context?Carnage
look to the functions listed by ob_list_handlers()Idel
@rambocoder ob_list_handlers() returns an array with 2 values: "default output handler"Coeternal
have you tried to increase buffer size? option output_buffering in php.ini fileRomalda
I hope this thread has not become a zombie yet. I see that you are talking about core drupal code here. Our drupal 7 site also is occasionally experiencing similar problem with ob_flush and interestingly inside drupal_page_footer() itself. Have you been able to get to the root of this issue? It would be great if you could share what you learned.Darcidarcia
X
2

Try this:

ob_start();
//Your code to generate the output
$result = ob_get_contents(); //save the contents of output buffer to a string
ob_end_clean();
echo $result;

It is run quick for me.

Xylon answered 3/2, 2014 at 22:2 Comment(0)
H
1

[You may want to tag your question with Drupal, since this feels like it might be a Drupal issue. Specifically, I suspect that when you flush the buffer, you're writing to an outer buffer, which triggers a ton of hooks to be called to filter the data you've just written.]

I suspect that your problem is nested buffers. Drupal really likes buffers and buffers everything all over the place. Check the result of:

echo "<pre>\nBuffering level: ";
    . ob_get_level() .
    . "\nBuffer status:\n"
    . var_dump(ob_get_status(TRUE))
    . "\n</pre>";

If you've got nested buffers, then I suspect ob_flush() will do nothing for you: it just appends the contents of your inner buffer into the next outermost layer of buffering.

Nested buffers can come from Drupal itself (which the above will show), or from the settings for zlib-output-compression and output_buffering (try twiddling those, see if it changes anything).

If your buffers are not nested, and the above settings do not help, then you may also want to split the operation into pieces, and run the profiler there, to see which part is taking the time:

$data = ob_get_contents(); // Return the contents of the output buffer.
ob_clean(); // Clean (erase) the output buffer.
ob_end(); // Close the buffer.
echo($data); // Output our data - only works if there's no outer buffer!
ob_start(); // Start our buffer again.

The question then becomes, though, "what are you trying to accomplish?" What do you think ob_flush() is doing here? Because if the answer is "I want to push everything I've done so far to the browser"... then I'm afraid that ob_flush() just isn't the right way.

Honeybunch answered 19/2, 2014 at 23:33 Comment(0)
M
-1

SET

output_buffering = Off

in php.ini

Misusage answered 14/11, 2013 at 21:5 Comment(0)
C
-2

use

<?ob_start();?>

at the beginning of the page and

 <?ob_flush();?>

at the end of the page, to solve this problem.

Connie answered 29/3, 2013 at 6:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.