以php的多进程实现类似多线程,并行执行脚本的代码:
#!/usr/bin/env php
<?php
/**
* php 多进程
* by www.jb200.com
*/
$cmds=array(
array('/app/test.php','charge/pstat.php','mobile',1),
array('/app/test.php','charge/pstat.php','mobile',2),
array('/app/test.php','charge/pstat.php','click',1),
array('/app/test.php','charge/pstat.php','click',2),
array('/app/test.php','charge/pstat.php',1),
array('/app/test.php','charge/pstat.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);
}
}
?>