多进程
$goingTasks = array(1,2,3,4,5,6,7); $max_chlid_fork = 4; foreach($goingTasks as $goingTask) { fork($goingTask);//子进程 } /** * 子进程 */ function fork($goingTask){ static $curChildPro = 1; global $max_chlid_fork; //$curChildPro++; _echo_msg(' Childs_Num:Now['.$curChildPro.']'); //多进程处理 $pid = pcntl_fork(); if($pid){ //父进程运行代码,达到上限时父进程阻塞等待任一子进程退出后while循环继续 _echo_msg(' This is Parent:Child('.$pid.')'); if($curChildPro >= $max_chlid_fork) { _echo_msg(' This is Parent:Childs Max['.$max_chlid_fork.'],Now['.$curChildPro.'] Wating...'); pcntl_wait($status); _echo_msg(' This is Parent:Childs Max['.$max_chlid_fork.'],Now['.$curChildPro.'] Going...'.$status); //$curChildPro--; }else{ $curChildPro++; } //继续下一个进程 }else if($pid == -1){ $curChildPro--; //报错 _echo_msg(' This is Child Woring!'); exit; }else{ //$curChildPro++; _echo_msg(' $pid ['.$pid.']'); _echo_msg(' $curChildPro ['.$curChildPro.']'); //---------子进程运行代码--------- $cpid = getmypid(); _echo_msg(' This is Child('.$cpid.') Begin['.date('Y-m-d H:i:s').']'); work($goingTask); _echo_msg(' This is Child('.$cpid.') End['.date('Y-m-d H:i:s').']'.PHP_EOL); exit;//子进程结束 需跳出 } } function work($goingOne){ while(true) { $return = dealOneTask($goingOne); if(!$return) { break; } } } function dealOneTask($goingOne) { sleep(60); } function _echo_msg($msg) { echo date('Y-m-d H:i:s') . $msg . PHP_EOL; }
连接数据库时, 需要重置连接
MySQLi
static function ResetConnect(){ //多进行脚本 需要重制链接 self::$links = array(); self::$masterDBInfos = array(); self::$slaveDBInfos = array(); }