PHP 多进程

php多进程使用pcntl_*系列函数

自己目前就知道一个 pcntl_fork() ,生成子进程

<?php
    header("Content-type:text/html;Charset=UTF8");
    /*
    echo '当前程序已经运行到此处!' . "
";
    $temp     = 1;
    //开始分进程
    $pid             = pcntl_fork();
    if($pid == -1) 
        exit('程序出错!');
    if($pid) {
        echo "我是父进程,当前temp的值为$temp
";
        $temp = 0;
    }else {
        sleep(1);
        if($temp == 0)
            echo "我是子进程,当前temp的值为$temp
";
        $temp = 1;
    }

    运行结果:
        当前程序已经运行到此处!
        我是父进程,当前temp的值为1
    说明:
        执行不到子进程,父进程跟子进程基本上是同时执行的。而且程序只运行一次。所以只能执行父进程
    */

    /*--------------------------*/
    /*
    //当前时间大于制定的时间时,给一个get值
    if(time() - strtotime('2014/08/05 16:02:00')) {
        $_GET['a'] = 1;
    }
    $temp = false;
    echo '当前程序已经运行到此处!' . "
";
    //开始分进程
    $pid             = pcntl_fork();
    var_dump($_GET);
    echo "上面是当前get的值
";
    if($pid == -1) 
        exit('程序出错!');
    if($pid) {    //父进程死循环监听
        do{
            if(isset($_GET['a'])) {
                $temp = true;
            }else{
                echo "我是父进程,一直在循环运行
";
                echo date('Y-m-d H:i:s') . "
";
            }
        }while(true);
    }else {
        while($temp) {
            echo "我是子进程,父进程更改了temp的值,我开始执行了!";
            $temp = false;
        }
    }
    结果:
        当前程序已经运行到此处!
        array(1) {
          ["a"]=>
          int(1)
        }
        上面是当前get的值
        array(1) {
          ["a"]=>
          int(1)
        }
        上面是当前get的值
    说明:
        $pid后程序分叉,后面的程序执行了2次,
        但是get的值为true,所有父进程改变了temp的值,
        但是同时子进程也运行了(temp的值在被父进程改变之前运行),所以子进程也不允许。
        跟上面的列子类似
    warning:
        系统资源占用大,跟上面的类似
    */

    /*--------网摘代码
    define("PC", 10); // 进程个数
    define("TO", 4); // 超时
    define("TS", 4); // 事件跨度,用于模拟任务延时

    if (!function_exists('pcntl_fork')) {
        die("pcntl_fork not existing");
    }

    // 创建管道
    $sPipePath = "my_pipe.".posix_getpid();
    if (!posix_mkfifo($sPipePath, 0666)) {
        die("create pipe {$sPipePath} error");
    }

    // 模拟任务并发
    for ($i = 0; $i < PC; ++$i ) {
        $nPID = pcntl_fork(); // 创建子进程
        if ($nPID == 0) {
            // 子进程过程
            //sleep(rand(1,TS)); // 模拟延时
            sleep(10);
            $oW = fopen($sPipePath, 'w');
            fwrite($oW, $i."
"); // 当前任务处理完比,在管道中写入数据
            fclose($oW);
            exit(0); // 执行完后退出
        }
    }

    // 父进程
    $oR = fopen($sPipePath, 'r');
    stream_set_blocking($oR, FALSE); // 将管道设置为非堵塞,用于适应超时机制
    $sData = ''; // 存放管道中的数据
    $nLine = 0;
    $nStart = time();
    while ($nLine < PC && (time() - $nStart) < TO) {
        $sLine = fread($oR, 1024);
        if (empty($sLine)) {
            continue;   
        }   
         
        echo "current line: {$sLine}
";
        // 用于分析多少任务处理完毕,通过‘
’标识
        foreach(str_split($sLine) as $c) {
            if ("
" == $c) {
                ++$nLine;
            }
        }
        $sData .= $sLine;
    }
    echo "Final line count:$nLine
";
    fclose($oR);
    unlink($sPipePath); // 删除管道,已经没有作用了

 
    // 等待子进程执行完毕,避免僵尸进程
    $n = 0;
    while ($n < PC) {
        $nStatus = -1;
        $nPID = pcntl_wait($nStatus, WNOHANG);
        if ($nPID > 0) {
            echo "{$nPID} exit
";
            ++$n;
        }
    }

    // 验证结果,主要查看结果中是否每个任务都完成了
    $arr2 = array();
    foreach(explode("
", $sData) as $i) {// trim all
        if (is_numeric(trim($i))) {
            array_push($arr2, $i);  
        }
    }
    $arr2 = array_unique($arr2);
    if ( count($arr2) == PC) {  
        echo 'ok'; 
    } else {
        echo  "error count " . count($arr2) . "
";
        var_dump($arr2);
    }
    */

以上是自己测试的代码,基本没有学到什么有价值的东西。

网摘代码的源地址:http://www.cnblogs.com/bourneli/archive/2012/07/06/2579893.html

原文地址:https://www.cnblogs.com/lxdd/p/3892920.html