Esempio n. 1
0
 /**
  * 等待应答
  *
  * @param  integer $timeout   请求超时时间(毫秒)
  * @param  boolean $autofetch 是否自动获取结果
  * @param  boolean $keep      是否保留结果
  * @return integer            未收到应答的请求数
  */
 public function wait_for_replies($timeout = -1, $autofetch = TRUE, $keep = FALSE)
 {
     //页面清除cache
     //是否开启页面清除cache
     $open_clear_page_cache = APF::get_instance()->get_config('open_clear_page_cache');
     //要接受的getkey
     $clear_page_cache_key = APF::get_instance()->get_config('clear_page_cache_key');
     //要接受的getval
     $clear_page_cache_val = APF::get_instance()->get_config('clear_page_cache_val');
     if ($open_clear_page_cache) {
         //如果接受设定的get方法中有值不为空,则清空当前cache
         if (APF::get_instance()->get_request()) {
             $isclearcache = APF::get_instance()->get_request()->get_parameter($clear_page_cache_key);
             if ($isclearcache == $clear_page_cache_val) {
                 apf_require_class('GlobalFunc');
                 //判断ip段
                 if (GlobalFunc::is_allow_debug()) {
                     $timeout = -1;
                 }
             }
         }
     }
     try {
         if (APS::get_instance()->get_zmq_enabled()) {
             $poll = new ZMQPoll();
             //            foreach (self::$sockets as $socket) {
             //                $poll->add($socket, ZMQ::POLL_IN);
             //            }
             $poll->add($this->socket, ZMQ::POLL_IN);
             $start = APS_Functions::aps_millitime();
             $pending = count($this->requests);
             $readable = $writeable = array();
             while ($pending > 0 && $timeout > 0) {
                 $events = $poll->poll($readable, $writeable, $timeout);
                 if ($events == 0) {
                     break;
                 }
                 foreach ($readable as $socket) {
                     $this->process_reply($socket);
                     --$pending;
                 }
                 $millitime = APS_Functions::aps_millitime();
                 $timeout -= $millitime - $start;
                 if ($timeout <= 0) {
                     break;
                 }
                 $start = $millitime;
             }
         }
         if ($autofetch) {
             $this->fetch_all_replies($keep);
             $pending = 0;
         }
     } catch (Exception $e) {
         error_log(print_r('Caught exception: ' . $e->getMessage() . "\n", 1), 3, '/tmp/replaceWords.log');
     }
     return $pending;
 }