php多进程并行执行脚本的例子

发布时间:2020-11-12编辑:脚本学堂
有关php实现多进程并行执行脚本的方法,php的进程是不支持多线程的,用php实现多进程以弥补不足,需要的朋友参考下。

例子,php进程不支持多线程,用php实现多进程。

<?php
$cmds=array(
 array('/apps/bin/launcher.php','charge/promotion_props_stat.php','mobile',1),
 array('/apps/bin/launcher.php','charge/promotion_props_stat.php','mobile',2),
 array('/apps/bin/launcher.php','charge/promotion_props_stat.php','click',1),
 array('/apps/bin/launcher.php','charge/promotion_props_stat.php','click',2),
 array('/apps/bin/launcher.php','charge/promotion_props_stat.php',1),
 array('/apps/bin/launcher.php','charge/promotion_props_stat.php',2)
);
foreach($cmds as $cmd){
 $pid=pcntl_fork();
 if($pid==-1){ //进程创建失败
  die('fork child process failure!');
 }
 else if($pid){ //父进程处理逻辑
  pcntl_wait($status,WNOHANG);
 }
 else{ //子进程处理逻辑
  pcntl_exec('/usr/local/bin/php',$cmd);
 }
}