PHP實現多程序並行執行指令碼
阿新 • • 發佈:2019-01-26
由於php的程序是不支援多執行緒的,有些場景為了方便以及提高效能,可以用php實現多程序以彌補這個不足:
#!/usr/bin/env 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); } }