PHP pcntl_fork():在浏览器中丢失代码

时间:2012-07-09 15:23:26

标签: php pcntl

我想使用pcntl在PHP中实现bigpipe。 但是,当我从浏览器访问网页时出现了问题。

test.php的

<html>
    <head>
<script type="text/javascript">
    function fill(id,content){ document.getElementById(id).innerHTML=content; }
</script>
    </head>
    <body>
        <div id="1">loading...</div>
        <div id="2">loading...</div>    
<?php
    $pids = array();
    $pids[0] = pcntl_fork();
    if($pids[0]==-1){
        die("failed to fork\n");
    }else if($pids[0] == 0){//child
        sleep(1);
        echo "<script type=\"text/javascript\">fill(\"1\",\"im txx\");</script>\n";
        exit(0);
    }else if($pids[0] > 0){//father
        $pids[1] = pcntl_fork();
        if($pids[1]==-1){
            die("failed to fork\n");
        }else if($pids[1] == 0){//child
            echo "<script type=\"text/javascript\">fill(\"2\",\"im txx!\");</script>\n";
            exit(0);
        }else if($pids[1] >0){//father
            while(pcntl_wait($status)!=-1);
        }
    }
?>
    </body>
</html>

当我在shell中输入命令“php test.php”时,输出是正确的:

<html>
        <head>
<script type="text/javascript">
        function fill(id,content){ document.getElementById(id).innerHTML=content; }
</script>
        </head>
        <body>
                <div id="1">loading...</div>
                <div id="2">loading...</div>
<script type="text/javascript">fill("2","im txx!");</script>
<script type="text/javascript">fill("1","im txx");</script>
        </body>
</html>

但是当我通过浏览器访问它时,页面源是意外的:

<html>
    <head>
<script type="text/javascript">
    function fill(id,content){ document.getElementById(id).innerHTML=content; }
</script>
    </head>
    <body>
        <div id="1">loading...</div>
        <div id="2">loading...</div>    
<script type="text/javascript">fill("2","im txx!");</script>

为什么剩下的代码消失了?

1 个答案:

答案 0 :(得分:0)

您在打印后退出:

exit(0);

这将阻止超出此点的所有代码的执行

删除exit()命令