tags:

views:

38

answers:

2

I'm currently running these lines in a PHP script:

foreach ($list as $route)
{
exec('php-cgi ./nextbus-route_stop_predictions.php route=' . $route['route_id']);
}

nextbus-route_stop_predictions.php takes about 15 seconds to run and exec() is ran about 12 times. I was wondering if PHP was able to run these those command lines without needing to wait for the output of the previous exec(). So basically, I want to run this asynchronously/multi-processes.

+2  A: 

pcntl_fork() may be able to help if you're on *nix.

The pcntl_fork() function creates a child process that differs from the parent process only in its PID and PPID. Please see your system's fork(2) man page for specific details as to how fork works on your system.

Mike B
I've used it many times, and must say that IF your work is highly parallelized (meaning you don't need inter-process communication or synchronization), it works VERY well. I use it to parse around 500 XML files at once (each is around 1gb). I simply fork a child for each process (well, using a process pool) and let it run unit it exits...
ircmaxell
Not exactly sure how this is run, but do I just $pid = pcntl_fork() in my for loop?
Steven Lu
A: 

If you are running php as an Apache module you can do the trick mentioned here

http://joseph.randomnetworks.com/archives/2005/10/21/fake-fork-in-php/

exec("php-cgi ./nextbus-route_stop_predictions.php route=" . $route['route_id'] . " 2>/dev/null >&- < &- >/dev/null &");

Basically rerouting stderr,stdout and sending the process into the background.

David Young