Running PHP exec() asynchronously/multi-processes
I'm currently running these lines in a PHP script:
foreach ($list as $route)
{
exec('php-cgi ./nextbus-r开发者_JS百科oute_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.
Update
For anyone still looking for the answer, I used nohup
and piped the output to /dev/null
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.
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.
精彩评论