I have code that's somewhat like this:
($i=0; $i < 100; $i++)
{
do ($foo[$i]);
}
The above is a time intensive task, and I was hoping to be able to create a function, and call it twice like below
function wrapper($start;$end)
{
($i=$start; $i < $end; $i++)
{
do ($foo[$i]);
}
}
//have both of these run in parallel
wrapper(0,50);
wrapper(51,100);
I looked at Gearman but I cannot use it as I cannot install the gearman server (since I'm on a shared server). It seems like the way to achieve this would be by forking. I tried reading up a lot about it, but documentation and support is scant. Any help / wireframe code would be appreciated.
To define my question, how could I call wrapper()
passing in arguments such that it executes in a child process. Also, its important that I be able to register a callback function.
Additional Details: PHP 5.3, running on Linux server. Script is executed by cgi-fcgi.
I think this is how I am supposed to spawn a child process, but how can I use it to spawn multiple child processes? And how do I register a callback function?
$pid = pcntl_fork();
if ( $pid == -1 ) {
// Fork failed
exit(1);
} else if ( $pid ) {
// The parent process
//should I repeat this same code here to spawn another child process?
} else {
// the child process
//can I call wrapper from here and will it run in this child process?