ninja9578
March 24th, 2011, 02:05 PM
See the below code. I have a web page that runs this PHP, so one would assume that the parent process can echo to the page, but its the child that seems to be. It appears that pcntl_fork is forking, but handing the pid off to the child process, and returning zero to the parent. Why is this? Is there another way to do asyncronous PHP?
private function _runTest($count, array & $pids){
if ($count >= self::FORKS) return;
$pid = pcntl_fork();
var_dump($pid);
if ($pid == -1){
$this -> fail('fork failed');
} else if ($pid){ //this is the parent
echo 'Parent';
$pids[] = $pid;
$this -> _runTest($count + 1, $pids);
} else {
echo 'Child (shouldnt see this echo)';
try {
sleep(rand(0, 5));
file_put_contents('/tmp/config.json', file_get_contents(dirname(__FILE__) . '/Resources/good.json'));
$this -> _assertGood();
exit(0);
} catch (Exception $e){
exit(1);
}
}
}
private function _runTest($count, array & $pids){
if ($count >= self::FORKS) return;
$pid = pcntl_fork();
var_dump($pid);
if ($pid == -1){
$this -> fail('fork failed');
} else if ($pid){ //this is the parent
echo 'Parent';
$pids[] = $pid;
$this -> _runTest($count + 1, $pids);
} else {
echo 'Child (shouldnt see this echo)';
try {
sleep(rand(0, 5));
file_put_contents('/tmp/config.json', file_get_contents(dirname(__FILE__) . '/Resources/good.json'));
$this -> _assertGood();
exit(0);
} catch (Exception $e){
exit(1);
}
}
}