如何使用php和pcntl函数派生进程返回数据?

时间:2015-08-28 12:27:50

标签: php pcntl

我想要实现的是分叉进程并在子进程中执行webservice /数据库查询,当数据返回到子进程时,我需要在页面上显示它。我对此有几个问题。

如何将返回的数据返回到调用fork进程的脚本?

我如何能够轮询以查看是否有任何结果返回?这只是在子进程返回并以某种方式检查它之后在父进程中将布尔变量设置为true的情况吗?

对此的任何帮助将不胜感激。

<?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 "\npid: ".$pid;
    if ($pid == -1) { 
       die("could not fork"); 
    } elseif ($pid) { 
        echo "\nI'm the Parent (".$pid.") "; 
        pcntl_waitpid($pid, $status, WUNTRACED);
        echo "\nchild process returned"; 
    } else { 
        /*
            Do webservice / database query here
        */
       echo "\nI am the child - sleeping for 20 secs"; 
        sleep(20); 
       echo "\nreturning to parent now \n\n\n"; 
    } 

    exit(0);
?>

0 个答案:

没有答案