Exemple #1
0
 /**
  * 创建一个子进程
  * @param Worker $worker
  * @throws Exception
  */
 public function fork_one_worker()
 {
     //$sockets = stream_socket_pair(STREAM_PF_UNIX, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP);
     $pid = pcntl_fork();
     // 父进程,$pid 是子进程的id
     if ($pid > 0) {
         self::$_worker_pids[$pid] = $pid;
     } elseif (0 === $pid) {
         $this->set_process_title($this->title);
         $this->set_process_user($this->user);
         self::$_worker_pids = array();
         //$this->uninstall_signal();
         $pid = posix_getpid();
         //echo "worker[".$pid."] running\n";
         if ($this->on_worker_start) {
             call_user_func($this->on_worker_start, $this);
         }
         // 这里用0表示正常退出
         exit(0);
     } else {
         exit("fork one worker fail");
     }
 }
Exemple #2
0
 /**
  * 创建一个子进程
  * @param Worker $worker
  * @throws Exception
  */
 public function fork_one_worker($worker_id)
 {
     //$sockets = stream_socket_pair(STREAM_PF_UNIX, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP);
     $pid = pcntl_fork();
     // 主进程记录子进程pid
     if ($pid > 0) {
         self::$_worker_pids[$worker_id] = $pid;
     } elseif (0 === $pid) {
         $this->time_start = microtime(true);
         $this->worker_id = $worker_id;
         $this->worker_pid = posix_getpid();
         $this->set_process_title($this->title);
         $this->set_process_user($this->user);
         // 清空master进程克隆过来的worker进程ID
         self::$_worker_pids = array();
         //$this->uninstall_signal();
         // 设置worker进程的运行状态为运行中
         self::$_status = "running";
         // 注册进程退出回调,用来检查是否有错误(子进程里面注册)
         register_shutdown_function(array($this, 'check_errors'));
         // 如果设置了worker进程启动回调函数
         if ($this->on_worker_start) {
             call_user_func($this->on_worker_start, $this);
         }
         // 停止当前worker实例
         $this->stop();
         // 这里用0表示正常退出
         exit(0);
     } else {
         log::add("fork one worker fail", "Error");
         exit;
     }
 }