I am wondering how to handle operations that have a very long execution time, such as completely rebuilding a searchindex-table in a database.

An operation such as this one can take a very long time, so how do I prevent a timeout? I know I can use the set_time_limit() function so at least my script can run until completion, but I guess the browser will give up after a while since I am not sending any output. And even if I would, I suspect it will timeout anyway after a certain amount of time.

I could run my script directly from the commandline, but I'd like this functionality to be available from the administation backend.

Any information would be greatly appreciated!

    The best way I found to do this is to just continually dump output to the browser to let it know that you are still producing output.

    Any lengthy scripts I have dump out a couple of space chars to output everytime it goes round an internal loop - each permutation of the loop could take upto 30 seconds, but the browser doesn't generally time out in this short amount of time.

    Since consecutive spaces are ignored in HTML, this also has no effect on any HTML layout you may have 🙂

    BTW - this assumes that you do not have output buffering switched on in PHP.

      Originally posted by bobthedog
      The best way I found to do this is to just continually dump output to the browser to let it know that you are still producing output.

      Any lengthy scripts I have dump out a couple of space chars to output everytime it goes round an internal loop - each permutation of the loop could take upto 30 seconds, but the browser doesn't generally time out in this short amount of time.

      Since consecutive spaces are ignored in HTML, this also has no effect on any HTML layout you may have 🙂

      BTW - this assumes that you do not have output buffering switched on in PHP.

      Yes, this method did indeed cross my mind. I do have output buffering turned on, but I could try switching it off. What's the most common behaviour for browsers? Do they reset their timeout-counter when they receive any amount of data? If this is the case, I could use this trick to double as a progress-indicator by printing e.g. a percentage-string.

        Originally posted by Wave
        check this link.

        http://us.php.net/manual/en/features.connection-handling.php

        Thanks for the link! I read this article before, and it directed me towards the set_time_limit() function. By setting the timelimit to 0 full, uninterrupted execution of the script is guaranteed, but the browser could still trigger a time-out. And that's the problem I am facing at the moment.

          i have an excellent idea...

          may be running this script from crontab is a better way?
          just let your script make all output to log file, and check it later.

            Originally posted by Wave
            i have an excellent idea...

            may be running this script from crontab is a better way?
            just let your script make all output to log file, and check it later.

            Good idea, but this would basically be the same as running it from the command-line. I'd like this functionality to be available to administrators in the administration backend.

            However, if I don't find a workable solution to my problem I will make it available via the command-line or crontab only.

              in any case you must to check your database periodically. so let cron do this job for you, and let your administrators check how clear this job has been done 🙂

                As long as the browser is receiving text, I dont think it cares how long th epage loading goes on. The only reason a browser times out is cos either the HTTP connection gets dropped between it and the server, or because no ouput has been received for X amount of time.

                As long as your script doesn't time out (set_time_limit(0)), and it produces some output to keep the browser happy, then it should all go smoothly.

                As a point though - if you want a kind of percentage thing to happen, I think you're gonna have to do some crafty Javascript work to update a variable when you dump more output out which in turns refreshed the display on the browser.

                Another thing to bear in mind with this is that the browser caches all output until either the stream is broken, or it's buffer gets full. It then flushes output to the display and resets the buffer. You can get around this by filling the buffer till it flushes (with spaces or somet like that - since spaces wont display in browser). This is a code snippet I used before when sending 1000's of emails:

                
                /*Dump some page output out up here*/
                
                
                // Let this script execute forever if it has to
                set_time_limit(0);
                
                // Make sure that the buffer is flushed and the browser does not cache any content so thatthe page output will display before we begin our work
                print(str_repeat(" ", 300) . "\n");
                flush(); // Flush all output to make sure
                
                // Send the mails out - this func sends regular output (spaces) to the browser to keep the browser from timing out
                $arrValues = do_send_campaign_mail($campaign, $sendto, $value, $S_cid, true);
                
                // when the mails are finished then send the page somewhere else
                print("<script language=\"Javascript\">
                <!--
                  document.location='finished.php';
                //-->
                </script>");
                
                

                Hope this helps

                  Originally posted by bobthedog
                  As long as your script doesn't time out (set_time_limit(0)), and it produces some output to keep the browser happy, then it should all go smoothly.

                  The problem is that this won't be any good if PHP itself is waiting for some external program to do something (like a reindexing DBMS). PHP will just sit on the system call and block until the other program finishes.

                  There is on *nix systems the at command, which works like a one-off cron job (see its man page). If PHP told the system to run a cron-like job "at" a certain time (like five seconds from now), it would return (from at) just about immediately.

                  The drawback of course is that you then lose the chance of PHP's getting any information back from the job (like when it finishes).

                    Write a Reply...