|
Posted by C. (http://symcbean.blogspot.com/) on 01/07/08 12:26
On 7 Jan, 11:56, Jerry Stuckle <jstuck...@attglobal.net> wrote:
> Paul Fisch wrote:
> > This might not be the correct place for this question, if not if
> > someone could point me in the right direction that would be good.
>
> > Ok, let's say you have an html document that calls a php script. Now
> > the php script doesn't even necessarily have to output any information
> > to the client, but lets just say that all it does is some very complex
> > sql stuff involving large tables that takes as long as 10 minutes to
> > execute. The script includes set_time_limit(1500). Now if someone
> > views the html page for 10-15 seconds and then closes the page will
> > the php script continue and execute until completion even though the
> > client has long since closed the page? Also could this work using
> > ajax if a javascript script then triggers a php script?
>
> > Thanks,
> > Paul Fisch
>
> Paul,
>
> First thing - you have two different timeouts to worry about. The first
> one is the PHP script itself, which you can change with set_time_limit()
> (if the server allows it). The other limit is the browser, which you
> can't change in PHP.
>
> Now, you can start a batch script to execute your long-running code.
> But normally it will execute synchronously - that is, your web server
> script will wait for the batch script to finish. During this time your
> existing script won't complete its execution, so the user won't be able
> to continue (and the browser may time out).
>
> You can start the batch script as a background task. As a background
> task, the script will not be able to display any output, nor will it be
> able to return results to the user (because the web server script has
> moved on). But the web server script will complete processing and
> return to the user.
>
> Of course, another possibility is to queue up the work to be done (i.e.
> in a database) and run a cron job on a regular basis to check the queue
> and process anything waiting. This works great if there is no need to
> process the data immediately.
>
> Hope this gives you some ideas.
>
> --
Nobody seems to have mentioned that:
1) there is a whole chapter in the manual on this :
http://www.php.net/manual/en/features.connection-handling.php
2) you should also set ignore_user_abort(true); as detailled in the
above.
The reason its a bad idea to do this via the webserver is that the
webserver itself may impose additional constraints in how long a
process runs for, and what resources it accesses. Its also difficult
to manage the resource intensive script independently of the
webserver. A better solution is to run it in the CLI PHP. Try googling
for PHP "long running process" for lots of information.
C.
[Back to original message]
|