Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is PHP flush not always working, page 1/5 times not loaded properly?

Tags:

php

apache

A large php script uses flush to send a part of the generated html to the browser while it is executing the larger part of the script which takes appr. 20 seconds.

The part of the script that is being executed after the flush is pretty large and takes a lot from the server reserves (90% cpu).

Once in 5 times the page stays blanc (white) which means that the flush didn't arrive to send the image that shows the page is loading. The page stays blanc (browser indicates it is still loading) also when the program is finished and should send the whole page to the browser.

Remarkable: When I press the back button the whole page would show (that should have been loaded in the browser after whole script is executed) for a second and then the browser goes back to the previous page.

All the other 4 times the page loads well. The input for the php script is the same for all 5 times.

I have these settings in my .htacces file:

addhandler x-httpd-php5-cgi .php5 
addhandler x-httpd-php5-cgi .php5
addhandler x-httpd-php5-cgi .php5 
Header Set Cache-Control "max-age=0, no-store"

This is the statement to flush the generated code to the browser:

print str_pad('',4096)."\n";
ob_flush();
flush();

What could be causing this problem?

like image 731
BastiaanWW Avatar asked Oct 08 '22 21:10

BastiaanWW


1 Answers

You can only hint: the flush() sends it to apache/webserver, which can buffer/wait, which sends it to the network with any number of proxies which can buffer/wait, which ultimately ends up in your browser, which can also decide to buffer/wait. Go for an asynchronous method if you don't want to deal with all that those headaches. Gearman makes it very easy, but not strictly necessary of course.

like image 169
Wrikken Avatar answered Oct 12 '22 12:10

Wrikken