ob_flush takes long time to be executed

▼魔方 西西 提交于 2019-12-22 03:44: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?


回答1:


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.




回答2:


[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.




回答3:


SET

output_buffering = Off

in php.ini




回答4:


use

<?ob_start();?>

at the beginning of the page and

 <?ob_flush();?>

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



来源:https://stackoverflow.com/questions/13903970/ob-flush-takes-long-time-to-be-executed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!