doutou7740 2015-08-28 12:27
浏览 42

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

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);
?>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
    • ¥88 实在没有想法,需要个思路
    • ¥15 MATLAB报错输入参数太多
    • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
    • ¥15 有赏,i卡绘世画不出
    • ¥15 如何用stata画出文献中常见的安慰剂检验图
    • ¥15 c语言链表结构体数据插入
    • ¥40 使用MATLAB解答线性代数问题
    • ¥15 COCOS的问题COCOS的问题
    • ¥15 FPGA-SRIO初始化失败