0

I am trying to output an update string when the loop is running to let the user know that the page is not stuck and it is running.

My code is running on google app engine and the loop part is like this.

while (($data = fgetcsv($file, 10000, ",")) !== FALSE) {
$STH = $DBH->prepare("INSERT INTO DB");
$STH->execute();
echo "<p>Please wait. How about a glass of water while it is running? </p>";
}

It is part of a code that checks a list of users from a CSV file and makes sure the DB is updated with the latest change. So this part is the insert part. What happens now is that it will not display the echo statement until the whole script is executed and I end up with a lot of lines of text.

I was wondering if we could output the echo when the loop is being executed. When I googled it seems that flush() and ob_flush() were used but it didn't work with google app engine. Some said to use task queues but cant find a decent tutorial that explains how to use task queues.

I was hoping some one would be able to help create one for novice users who want to use task queues. I am sure it will be very helpful when a person is running a loop and getting the output as it runs.

links for google documentation

https://cloud.google.com/appengine/docs/php/taskqueue/overview-push https://cloud.google.com/appengine/docs/php/config/queue

Abilash Amarasekaran
  • 817
  • 2
  • 10
  • 26

1 Answers1

1

This is not how HTTP works. You could rewrite your webserver's code and also write your own browser to support your own special streaming version of the Hypertext Transfer Protocol (defined in RFC 2616) which doesn't require a finite response to be sent... or you could do what a sane person would do and just:

  1. send back a response with a special key which the user's browser can use when sending tiny requests momentarily to the server

  2. send a tiny request to the server every few seconds to figure out if the computation the user triggered has finished (identified by the special key passed back when the computation was kicked off in the first request).

  3. At the time you receive an OK message from the server (for example, send back json responses and the success response will be { "success": true }), update the page with the result data or a link of where it's accessible, etc.

I would spend at least 3 more months researching how the internet works at its most basic level before attempting to go further in coding on a platform that assumes that basic knowledge. I'd suggest starting here.

DT Rush
  • 171
  • 1
  • 10
  • 1
    I was looking for something like this http://stackoverflow.com/questions/7232996/display-the-output-while-looping-in-php only that it works in google app engine. – Abilash Amarasekaran Jan 28 '15 at 19:05
  • http://stackoverflow.com/questions/22267130/how-to-flush-html-to-the-user-in-gae-php See, it can't work :). Sorry – Patrice Jan 28 '15 at 20:31