RE: Run a script apart from request

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



> -----Original Message-----
> From: Brad Fuller [mailto:bfuller@xxxxxxxxxxxxxxxx]
> Sent: Monday, April 30, 2007 10:55 AM
> To: php-general@xxxxxxxxxxxxx
> Subject:  Run a script apart from request
> 
> Hi all,
> 
> I am developing a program that does some intensive data processing,
and is
> triggered from a page hit and/or a SOAP request.
> 
> The processing takes on average 30 seconds to 1 minute, which is OK
for a
> web page (I can use set_time_limit(0) and just display a
pseudo-progress bar
> animated gif and a "Please wait..." message) but I can't leave the
SOAP
> request hanging for that long.  I need to send a response immediately.
> 
> What I really need to do is acknowledge the client that their request
has
> been received and will be processed, and terminate that request... and
THEN
> begin the processing.
> 
> One way I thought of doing it would be to put the requests into the
database
> and run a cron job every X minutes, but I would like to avoid this if
at all
> possible.
> 
> Someone had suggested pcntl_fork() but I'm not sure if that will
accomplish
> what I need it to... if I fork(), then send a response, kill the
parent and
> let the child run the process, will the HTTP request wait for the
child or
> will it die with the parent (like I want it to) ?
> 
> Any advice is much appreciated.
> 
> Thx,
> 
> Brad
> 
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php

I am not completely familiar with how forking works in PHP applications,
but I am fairly familiar with how it works in UNIX.  If you kill the
parent process, I believe it will close the HTTP request, but since it
doesn't handle the SIGCHLD signal after the child is done, the child
process will be left as a zombie process.  I believe the only way to
catch the SIGCHLD is to have the script pcntl_waitpid(), which would
then hang the HTTP request.

I haven't tested this, but you can easily test it by:

$pid = pcntl_fork();
 if($pid == -1) {
  // Something went wrong (handle errors here)
 } elseif($pid == 0) {
  // This part is only executed in the child
  usleep(5000000); //wait 5 seconds... see if the request hangs here
 } else {
  die(); 
 }

Then wait and see what happens.


Maybe there is someone more experience in this area that can put a few
words in, but that is what I believe to be the case.

-Logan

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux