0

I'd like to implement bigpipe in PHP using pcntl. However, there is something wrong when I visit the web page from browser.

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>

when I type command "php test.php" in shell, the output is correct:

<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>

But when I visit it by browser, the page source is unexpected:

<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>

Why the rest of code disappeared?

hakre
  • 193,403
  • 52
  • 435
  • 836
  • 1
    **If** process control works at all in your PHP installation, then the (CGI?) output pipe will be severed from the webserver. The second `echo` thus won't return page content. – mario Jul 09 '12 at 15:30
  • Like mario I'm dubious whether this would work under mod_php, cgi or fcgi invocation. And although forked processes will inherit file descriptors, there's no way of enforcing synchronization between the output streams - that you are then trying to dump the output into a file of highly stateful structure is overly optimistic at best. – symcbean Jul 09 '12 at 15:47

1 Answers1

0

You are exiting after printing:

exit(0);

this stops all execution of any code beyond this point

Remove the exit() command

hakre
  • 193,403
  • 52
  • 435
  • 836
Austin
  • 6,026
  • 2
  • 24
  • 24