0

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.

Update

For anyone still looking for the answer, I used nohup and piped the output to /dev/null

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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...
Not exactly sure how this is run, but do I just $pid = pcntl_fork() in my for loop?
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.