What I want to achieve is fork the process and in the child process do a webservice/database query and when the data returns to the child process I need to display it on the page. I have a few questions regarding this.
How do I get the returned data back to the script that invoked the fork process?
How would I be able to poll to see if any results have returned? Would this just simply be a case of setting a boolean variable to true in the parent process after the child has returned and somehow check for that?
Any help with this would be greatly appreciated.
<?php
// this script invokes the fork process from the web as it cannot be invoked directly from the web.
$testvar='testvalue';
shell_exec("/usr/bin/php5 /var/www/public_html/fork.php '?testvar=".$testvar."' &");
?>
<?php // this script does the forking
$pid = pcntl_fork();
echo "
pid: ".$pid;
if ($pid == -1) {
die("could not fork");
} elseif ($pid) {
echo "
I'm the Parent (".$pid.") ";
pcntl_waitpid($pid, $status, WUNTRACED);
echo "
child process returned";
} else {
/*
Do webservice / database query here
*/
echo "
I am the child - sleeping for 20 secs";
sleep(20);
echo "
returning to parent now
";
}
exit(0);
?>