Skip to content

php多进程示例

Posted on:2016年10月19日 at 12:30 (1 min read)

php 进程控制的扩展有两个pcntlposix,分别用于进程控制和信号控制,pcntl扩展只能在 cli 模式下使用,posixwindows 平台不支持。

多进程示例

if (substr(php_sapi_name(), 0, 3) !== 'cli') {
  die("This Programe can only be run in CLI mode");
}

$maxProcessNum = 10;
$subProcessNum = 0;

while(true){
  if($subProcessNum > $maxProcessNum){
    sleep(1);
    continue;
  }

  $pid = pcntl_fork();
  if($pid == 0){
    assignTask();
  }elseif($pid==-1){
    show("create subProcess fail");
  }else{
    $subProcessNum++;
    show("subProcess num:".$subProcessNum);
    if($subProcessNum>= $maxProcessNum){
      show("wait subProcess");
      pcntl_wait($status);
      $subProcessNum--;
    }
  }
}

function show($msg,$level="DEBUG"){
  echo sprintf("[%s] %s | %s\n",$level,posix_getpid(),$msg);
}

function assignTask(){
  $needTime = rand(1,10);
  show("Task Begin");
  sleep($needTime);
  show("Task End");
}

执行结果

[DEBUG] 10770 | subProcess num:1
[DEBUG] 10771 | Task Begin
[DEBUG] 10770 | subProcess num:2
[DEBUG] 10772 | Task Begin
[DEBUG] 10770 | subProcess num:3
[DEBUG] 10773 | Task Begin
[DEBUG] 10770 | subProcess num:4
[DEBUG] 10774 | Task Begin
[DEBUG] 10770 | subProcess num:5
...