0

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

测试.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>

为什么其余的代码消失了?

4

1 回答 1

0

打印后退出:

exit(0);

这将停止执行超出此点的任何代码

删除exit()命令

于 2012-07-09T15:27:40.463 回答