PHP-CLI多进程

  1. 安装PHP扩展:pcntl

  2. PHP源码

     <?php
     /**
      * 假设要执行命令
      * /usr/local/php7/bin/php /www/xxx.php --option 12345
      * /usr/local/php7/bin/php /www/xxx.php --option 12346
      * /usr/local/php7/bin/php /www/xxx.php --option 12347
      */
     $arr = [
         12345,
         12346,
         12347
     ];
     $exec_file = "/usr/local/php7/bin/php";
     foreach ($arr as $val) {
         $pid = pcntl_fork();
         if ($pid == -1) {
             exit(0);
         } elseif ($pid) {
             pcntl_wait($status, WNOHANG);
         } else {
             $command = ["/www/xxx.php", "--option", $val];
             pcntl_exec($exec_file, $command);
             exit(0);
         }
     }
     exit(0);
    

results matching ""

    No results matching ""