• PHP多进程处理并行处理任务实例(转,备用)


    本文目的
    本文通过例子讲解linux环境下,使用php进行并发任务处理,以及如何通过pipe用于进程间的数据同步。
    PHP多进程
    通过pcntl_XXX系列函数使用多进程功能。注意:pcntl_XXX只能运行在php CLI(命令行)环境下,在web服务器环境下,会出现无法预期的结果,请慎用!
    管道PIPE
    管道用于承载简称之间的通讯数据。为了方便理解,可以将管道比作文件,进程A将数据写到管道P中,然后进程B从管道P中读取数据。php提供的管道操作API与操作文件的API基本一样,除了创建管道使用posix_mkfifo函数,读写等操作均与文件操作函数相同。当然,你可以直接使用文件模拟管道,但是那样无法使用管道的特性了。
    僵尸进程
    子进程结束时,父进程没有等待它(通过调用wait或者waitpid),那么子进程结束后不会释放所有资源(浪费呀!),这种进程被称为僵尸进程,他里面存放了子进程结束时的相关数据,如果僵尸进程过多,会占用大量系统资源(如内存),影响机器性能。

    代码

    <?php
    
     
    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)); // 模拟延时
            $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);
    }
    # time php fork.php 
    current line: 1
    
    current line: 3
    
    current line: 4
    
    current line: 5
    
    current line: 8
    
    Final line count:5
    62732 exit
    62734 exit
    62735 exit
    62736 exit
    62739 exit
    62740 exit
    62731 exit
    62737 exit
    62738 exit
    62733 exit
    error count 5
    array(5) {
      [0]=>
      string(1) "1"
      [1]=>
      string(1) "3"
      [2]=>
      string(1) "4"
      [3]=>
      string(1) "5"
      [4]=>
      string(1) "8"
    }
    
    real    0m4.199s
    user    0m1.840s
    sys    0m1.554s
  • 相关阅读:
    MySql事务试什么?具有哪些特性
    MySQL的前缀索引你是如何使用的
    你知道联合索引吗?它的最左原则是什么?
    你来说一下 Mysql 索引有几种类型呢?分别是什么?
    什么是Mysql的回表查询?聊一聊
    JavaScript中使用typeof运算符需要注意的几个坑
    prototype和__proto__
    JavaScript中的普通函数与构造函数比较
    原型和原型链
    document.querySelector和querySelectorAll方法
  • 原文地址:https://www.cnblogs.com/zh718594493/p/12153713.html
Copyright © 2020-2023  润新知