示例#1
0
 public static function dispatch($timeout = self::DEFAULT_TIMEOUT)
 {
     $origin_timeout = $timeout;
     $poll = new \ZMQPoll();
     foreach (self::$sockets as $socket) {
         $poll->add($socket, ZMQ::POLL_IN);
     }
     $read = $write = array();
     while (count(self::$pendingRequests) > 0) {
         $_start = microtime(true);
         $events = $poll->poll($read, $write, self::INTERVAL);
         if ($events > 0) {
             foreach ($read as $socket) {
                 $recv = $socket->recvMulti();
                 $event = Response::deserialize($recv);
                 self::handleEvent($event);
             }
         }
         $_end = microtime(true);
         $timeout -= ($_end - $_start) * 1000;
         if ($timeout < 0) {
             break;
         }
     }
     if (count(self::$pendingRequests) > 0) {
         $exception = count(self::$pendingRequests) . " requests timeout after {$origin_timeout} ms:\n";
         foreach (self::$pendingRequests as $id => $pending) {
             $exception .= "  # {$pending['event']->name}\n";
         }
         throw new TimeoutException(trim($exception, "\n"));
     }
     $poll->clear();
     self::clear();
 }
示例#2
0
 public function sync($name, array $args, $timeout = 0)
 {
     if (!$timeout) {
         $timeout = $this->timeout;
     }
     $event = new Request($name, $args);
     $this->context->hookBeforeSendRequest($event, $this);
     $this->socket->sendMulti($event->serialize());
     $read = $write = array();
     $poll = new \ZMQPoll();
     $poll->add($this->socket, ZMQ::POLL_IN);
     $events = $poll->poll($read, $write, $timeout);
     if ($events) {
         $recv = $this->socket->recvMulti();
         $event = Response::deserialize($recv);
         return $event->getContent();
     } else {
         throw new TimeoutException('Timout after ' . $this->timeout . ' ms');
     }
 }