Using Yii2 I have implemented pcntl_fork() in one of my controllers and though the process is working as I need there are a few issues I would like to sort out.
Simplified logic
//Start timer marker
Loop(5x) {
sleep(1);
$pid = pcntl_fork();
if(!$pid) {
//Child Process
sleep(30);
//Child process Timer marker 30 seconds
//processing as expected
} else if ($pid == -1) {
//Error
} else {
//PARENT
//Parent timer marker loops 5x 1xloop seconds
}
}
//Redirect Timer marker at 5 seconds
return $this->redirect(['/account/account-details','accountId'=>Yii::$app->session['accountId']]);
By looking at the timer markers my loop is executing every second. The child process are executing after 30 seconds 31, 32, 33..etc
What is getting me is my redirect is marked at 5 seconds so I know if it hitting that point at 5 seconds. However the redirect does not execute until after the last child of 35 seconds. So it seems to hang there. I am guessing I am not using the right pcntl method to continue without waiting for children to finish.
And point in a direction would be helpful
Thanks