[nycphp-talk] How HTTP connection life relates to PHP script life
Flavio daCosta
nyphp at n0p.net
Tue Jun 21 09:04:03 EDT 2005
On 06/20/2005 11:20 PM, Jayesh Sheth wrote:
> Now for the questions:
> a) If a the user hits the stop button or navigates to another page, will
> Apache realize that the HTTP connection has been closed and thus
> terminate the script? If not, will it keep running, and if so for how
> long?
There is actually a php function to set this behavior:
<http://www.php.net/ignore_user_abort>
> b) I know this is not the best way to do it. What are your opinions on
> the following method: the PHP script should invoke a command line (CLI)
> script that runs as a separate process through the PHP CLI binary. That
> process writes how many emails it has sent to a MySQL table or to a
> file. The user's page simply META-refreshes, polls the MySQL table or
> log, and says 'x messages sent, y to go'.
Yes, I would agree with that idea. Due to the nature of the web, I
always strive to return control to the user as quickly as possible, less
_hourglass_ time. I like to do just as you are saying by decoupling the
time consuming process.
Over time I have used many techniques, invoking command in background,
Cron jobs monitoring a DB queue table, even a locally running daemon for
executing specific tasks once triggered. Also depending on the desired
end user waiting activity, I have implemented a few different behaviors:
- Message area embedded into the framework of the site, viewable while
they click through other pages. (e.g. area saying 13 of 200 done.)
- Reporting/Updates page where they can see the progress, and possibly
be emailed when complete.
- And recently, for short tasks, I have been returning the user to a
wait page and then utilizing AJAX to update the users in realtime, very
slick.
YMMV
Flavio
More information about the talk
mailing list