Beispiel #1
0
 /**
  * Run worker instance.
  * @return void
  */
 public function run()
 {
     //Update process state.
     self::$_status = self::STATUS_RUNNING;
     // Eegister shutdown function for checking errors.
     register_shutdown_function(array("\\Workerman\\Worker", 'checkErrors'));
     // Set autoload root path.
     Autoloader::setRootPath($this->_autoloadRootPath);
     // Create a global event loop.
     if (!self::$globalEvent) {
         $eventLoopClass = "\\Workerman\\Events\\" . ucfirst(self::getEventLoopName());
         self::$globalEvent = new $eventLoopClass();
         // Register a listener to be notified when server socket is ready to read.
         if ($this->_socketName) {
             if ($this->transport !== 'udp') {
                 self::$globalEvent->add($this->_mainSocket, EventInterface::EV_READ, array($this, 'acceptConnection'));
             } else {
                 self::$globalEvent->add($this->_mainSocket, EventInterface::EV_READ, array($this, 'acceptUdpConnection'));
             }
         }
     }
     // Reinstall signal.
     self::reinstallSignal();
     // Init Timer.
     Timer::init(self::$globalEvent);
     // Try to emit onWorkerStart callback.
     if ($this->onWorkerStart) {
         try {
             call_user_func($this->onWorkerStart, $this);
         } catch (\Exception $e) {
             echo $e;
             exit(250);
         }
     }
     // Main loop.
     self::$globalEvent->loop();
 }
Beispiel #2
0
 /**
  * 运行worker实例
  */
 public function run()
 {
     //更新 Worker 状态
     self::$_status = self::STATUS_RUNNING;
     // 注册进程退出回调,用来检查是否有错误
     register_shutdown_function(array("\\Workerman\\Worker", 'checkErrors'));
     // 设置自动加载根目录
     Autoloader::setRootPath($this->_appInitPath);
     // 如果没有全局事件轮询,则创建一个
     if (!self::$globalEvent) {
         $eventLoopClass = "\\Workerman\\Events\\" . ucfirst(self::getEventLoopName());
         self::$globalEvent = new $eventLoopClass();
         // 监听_mainSocket上的可读事件(客户端连接事件)
         if ($this->_socketName) {
             if ($this->transport !== 'udp') {
                 self::$globalEvent->add($this->_mainSocket, EventInterface::EV_READ, array($this, 'acceptConnection'));
             } else {
                 self::$globalEvent->add($this->_mainSocket, EventInterface::EV_READ, array($this, 'acceptUdpConnection'));
             }
         }
     }
     // 重新安装事件处理函数,使用全局事件轮询监听信号事件
     self::reinstallSignal();
     // 用全局事件轮询初始化定时器
     Timer::init(self::$globalEvent);
     // 如果有设置进程启动回调,则执行
     if ($this->onWorkerStart) {
         try {
             call_user_func($this->onWorkerStart, $this);
         } catch (\Exception $e) {
             echo $e;
             exit(250);
         }
     }
     // 子进程主循环
     self::$globalEvent->loop();
 }