在某些情況下,使用 pcntl_fork() 可能會有點棘手。對於快速作業,子程序可能會在父程序執行與程序啟動相關的程式碼之前完成處理。父程序可能會在其準備好處理子程序狀態之前收到訊號。為了處理這種情況,我會在訊號處理程式中將 id 新增到需要清理的程序「佇列」中,如果父程序尚未準備好處理它們的話。
<?php
declare(ticks=1);
class JobDaemon{
public $maxProcesses = 25;
protected $jobsStarted = 0;
protected $currentJobs = array();
protected $signalQueue=array();
protected $parentPID;
public function __construct(){
echo "constructed \n";
$this->parentPID = getmypid();
pcntl_signal(SIGCHLD, array($this, "childSignalHandler"));
}
public function run(){
echo "Running \n";
for($i=0; $i<10000; $i++){
$jobID = rand(0,10000000000000);
$launched = $this->launchJob($jobID);
}
while(count($this->currentJobs)){
echo "Waiting for current jobs to finish... \n";
sleep(1);
}
}
protected function launchJob($jobID){
$pid = pcntl_fork();
if($pid == -1){
error_log('Could not launch new job, exiting');
return false;
}
else if ($pid){
$this->currentJobs[$pid] = $jobID;
if(isset($this->signalQueue[$pid])){
echo "found $pid in the signal queue, processing it now \n";
$this->childSignalHandler(SIGCHLD, $pid, $this->signalQueue[$pid]);
unset($this->signalQueue[$pid]);
}
}
else{
$exitStatus = 0; echo "Doing something fun in pid ".getmypid()."\n";
exit($exitStatus);
}
return true;
}
public function childSignalHandler($signo, $pid=null, $status=null){
if(!$pid){
$pid = pcntl_waitpid(-1, $status, WNOHANG);
}
while($pid > 0){
if($pid && isset($this->currentJobs[$pid])){
$exitCode = pcntl_wexitstatus($status);
if($exitCode != 0){
echo "$pid exited with status ".$exitCode."\n";
}
unset($this->currentJobs[$pid]);
}
else if($pid){
echo "..... Adding $pid to the signal queue ..... \n";
$this->signalQueue[$pid] = $status;
}
$pid = pcntl_waitpid(-1, $status, WNOHANG);
}
return true;
}
}