海运的博客

PHP多进程Class类

发布时间:April 26, 2014 // 分类:PHP // No Comments

<?php
   declare(ticks=1);
   //A very basic job daemon that you can extend to your needs.
   class JobDaemon{

      public $maxProcesses = 8;
      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"));
      }

      /**
      * Run the Daemon
      */
      public function run(){
         echo "Running \n";
         for($i=0; $i<500; $i++){
            $jobID = rand(0,10000000000000);

            while(count($this->currentJobs) >= $this->maxProcesses){
               echo "Maximum children allowed, waiting...\n";
               sleep(1);
            }

            $launched = $this->launchJob($jobID);
         }

         //Wait for child processes to finish before exiting here
         while(count($this->currentJobs)){
            echo "Waiting for current jobs to finish... \n";
            sleep(1);
         }
      }

      /**
      * Launch a job from the job queue
      */
      protected function launchJob($jobID){
         $pid = pcntl_fork();
         if($pid == -1){
            //Problem launching the job
            error_log('Could not launch new job, exiting');
            return false;
         }
         else if ($pid){
            // Parent process
            // Sometimes you can receive a signal to the childSignalHandler function before this code executes if
            // the child script executes quickly enough!
            //
            $this->currentJobs[$pid] = $jobID;

            // In the event that a signal for this pid was caught before we get here, it will be in our signalQueue array
            // So let's go ahead and process it now as if we'd just received the signal
            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{
            //Forked child, do your deeds....
            $exitStatus = 0; //Error code if you need to or whatever
            echo "Doing something fun in pid ".getmypid()."\n";
            exit($exitStatus);
         }
         return true;
      }

      public function childSignalHandler($signo, $pid=null, $status=null){

         //If no pid is provided, that means we're getting the signal from the system.  Let's figure out
         //which child process ended
         if(!$pid){
            $pid = pcntl_waitpid(-1, $status, WNOHANG);
         }

         //Make sure we get all of the exited children
         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){
               //Oh no, our job has finished before this parent process could even note that it had been launched!
               //Let's make note of it and handle it when the parent process is ready for it
               echo "..... Adding $pid to the signal queue ..... \n";
               $this->signalQueue[$pid] = $status;
            }
            $pid = pcntl_waitpid(-1, $status, WNOHANG);
         }
         return true;
      }
   }
   $job = new JobDaemon();
   $job->run();

更多:http://blog.csdn.net/lvsmaster/article/details/6863951

标签:none

评论已关闭

分类
最新文章
最近回复
  • 海运: 正常情况下编译整个内核执行make menuconfig后就不会出现此提示,当单独编译单个模块...
  • oijq: 就是用的armbian的配置文件哈,按你的教程做的,在执行make LOCALVERSION=...
  • 海运: 使用armbian的配置文件,其它添加或修改自己懂的部分,不懂的就不要碰了。
  • oijq: 编译时这些选项全部选Y吗?Actions Semi Platforms (ARCH_ACTIO...
  • 海运: n1编译bbr内核模块参考这个:https://www.haiyun.me/archives/...
  • jiqz: make M=net/ipv4/ CONFIG_TCP_CONG_BBR=m modules编...
  • ruralhunter: 哦,文档里应该是对的,是.config
  • ruralhunter: cp /mnt/boot/config-4.18.7-aml-s9xxx .config这里应...
  • 海运: 你是编译不成功呢?还是编译后不能运行呢?还是运行后不能访问web界面呢?
  • 白墨: 可能不清楚就是编译安装后启动后访问不了web界面