Take the messages from the queue and if there are none wait 30
        seconds till returning an empty message.

        Also, cogen's wsgi async extensions are in the environ and prefixed with
        'cogen.'
425437
def pull(self, id):
        
        conn = session['connections'].get(id, None)
        conn.last_update = time.time()
        if conn:
            ev_list = []
            while 1:
                # ok, so this might look a bit ugly but the concept is very simple
                #  you yield a special object from the environ that does some magic
                #  and the wsgi server will resume the app when it has the result
                yield request.environ['cogen.call'](conn.events.get)(timeout=0.1)
                event = request.environ['cogen.wsgi'].result
                # also, we can't have better exception handling in this wsgi
                # contraption and we need to check the result for exceptions
                if not event:
                    break
                elif isinstance(event, (queue.Empty, events.OperationTimeout)):
                    break
                elif isinstance(event, Exception):
                    ev_list.append(('', 'ERROR', str(event)))
                    break
                else:
                    ev_list.append(event)
            if ev_list:
                print 'PULL:', ev_list
                yield simplejson.dumps(ev_list)
            else:
                # if we don't have any updates atm, we'll wait 30 secs for one
                yield request.environ['cogen.call'](conn.events.get)(timeout=30)
                event = request.environ['cogen.wsgi'].result
                if isinstance(event, events.OperationTimeout):
                    yield simplejson.dumps([])
                elif isinstance(event, Exception):
                    yield simplejson.dumps([('', 'ERROR', str(event))])
                else:
                    print 'PULL1:', event
                    yield simplejson.dumps([event])
        else:
            yield simplejson.dumps(('', 'ERROR', 'Invalid connection id.'))
294132
def get(self, timeout=None):
        """"""
        if timeout:
            try:
                result = self._queue.get(True, timeout)
            except multiprocessing.Queue.Empty:
                return(False, None)
            return(True, result)
        return self._queue.get()
952755
def get_new_checks(self, queue, return_queue):
        
        try:
            logger.debug("get_new_checks: %s / %s", len(self.checks), self.processes_by_worker)
            while len(self.checks) < self.processes_by_worker:
                msg = queue.get_nowait()
                if msg is None:
                    time.sleep(0.01)
                    continue
                logger.debug("Got a message: %s", msg)
                if msg.get_type() == 'Do':
                    logger.debug("Got an action: %s", msg.get_data())
                    self.checks.append(msg.get_data())
                    self.actions_got += 1
                elif msg.get_type() == 'ping':
                    msg = Message(_type='pong', data='pong!', source=self._id)
                    logger.debug("Queuing message: %s", msg)
                    return_queue.put_nowait(msg)
                    logger.debug("Queued")
                else:
                    logger.warning("Ignoring message of type: %s", msg.get_type())
        except Full:
            logger.warning("Actions queue is full")
        except Empty:
            logger.debug("Actions queue is empty")
            if not self.checks:
                self._idletime += 1
        # Maybe the Queue() has been deleted by our master ?
        except (IOError, EOFError) as exp:
            logger.warning("My actions queue is no more available: %s", str(exp))
            self.interrupted = True
        except Exception as exp:  # pylint: disable=broad-except
            logger.error("Failed getting messages in actions queue: %s", str(exp))

        logger.debug("get_new_checks exit")
1216981
def wait(self, timeout=None):
        
        # The 'or' part is for Python 2.6
        result = self.__event.wait(timeout)
        # pylint: disable=E0702
        # Pylint seems to miss the "is None" check below
        if self.__exception is None:
            return result
        else:
            raise self.__exception
74766
async def get(self):
        
        if not self._deque:
            self._ready.clear()
            await self._ready.wait()

        buffer = io.BytesIO()
        batch = []
        size = 0

        # Fill a new batch to return while the size is small enough,
        # as long as we don't exceed the maximum length of messages.
        while self._deque and len(batch) <= MessageContainer.MAXIMUM_LENGTH:
            state = self._deque.popleft()
            size += len(state.data) + TLMessage.SIZE_OVERHEAD

            if size <= MessageContainer.MAXIMUM_SIZE:
                state.msg_id = self._state.write_data_as_message(
                    buffer, state.data, isinstance(state.request, TLRequest),
                    after_id=state.after.msg_id if state.after else None
                )
                batch.append(state)
                self._log.debug('Assigned msg_id = %d to %s (%x)',
                                state.msg_id, state.request.__class__.__name__,
                                id(state.request))
                continue

            if batch:
                # Put the item back since it can't be sent in this batch
                self._deque.appendleft(state)
                break

            # If a single message exceeds the maximum size, then the
            # message payload cannot be sent. Telegram would forcibly
            # close the connection; message would never be confirmed.
            #
            # We don't put the item back because it can never be sent.
            # If we did, we would loop again and reach this same path.
            # Setting the exception twice results in `InvalidStateError`
            # and this method should never return with error, which we
            # really want to avoid.
            self._log.warning(
                'Message payload for %s is too long (%d) and cannot be sent',
                state.request.__class__.__name__, len(state.data)
            )
            state.future.set_exception(
                ValueError('Request payload is too big'))

            size = 0
            continue

        if not batch:
            return None, None

        if len(batch) > 1:
            # Inlined code to pack several messages into a container
            data = struct.pack(
                '<Ii', MessageContainer.CONSTRUCTOR_ID, len(batch)
            ) + buffer.getvalue()
            buffer = io.BytesIO()
            container_id = self._state.write_data_as_message(
                buffer, data, content_related=False
            )
            for s in batch:
                s.container_id = container_id

        data = buffer.getvalue()
        return batch, data
1140974
def wait(self, timeout=0):
        
        start_time = time.time()

        # We return synchronously, so we block in a busy loop waiting for the
        # request to be done.
        while not self.is_done:
            duration = time.time() - start_time
            if timeout and duration > timeout:
                raise CloudTimeoutError(
                    "Timeout getting async value. Timeout: %d seconds" % timeout
                )
            time.sleep(0.1)

        # If we get an any status code other than a 2xx we raise an exception to the user, which can then be handled
        # accordingly.
        status_code, error_msg, payload = self.check_error()
        if not self._status_ok(status_code):
            raise CloudAsyncError("Async response for '%s' returned an error." % self.async_id,
                                  reason=error_msg,
                                  status=status_code)

        value = self.value
        if isinstance(value, six.binary_type):
            value = value.decode('utf-8')
        return value
181272
def wait(self, timeout=None):
    

    if not self.__running:
      raise RuntimeError("ThreadPool ain't running")
    self.__queue.wait(timeout)
961792
async def send(self, message_type, message_content, timeout=None):
        
        return await self._sender.send(
            message_type, message_content, timeout=timeout)
599183
def wait_until(message='completed!')
      3.times { dot_and_sleep(1) }
      begin
        dot_and_sleep(2) until yield
      rescue Excon::Errors::SocketError => e
        # print out the error so the user can interrupt if necessary
        print "#{e.class}: #{e.message}! Retry:"
        sleep(1)
        retry
      end
      puts message
    end
219844
async def wait_for_body_middleware(environ, start_response=None):
    '''
    '''
    if environ.get('wsgi.async'):
        try:
            chunk = await environ['wsgi.input'].read()
        except TypeError:
            chunk = b''
        environ['wsgi.input'] = BytesIO(chunk)
        environ.pop('wsgi.async')
120182
def send_and_wait(self, path, message, timeout=0, responder=None):
        

        message.on("response", lambda x,event_origin,source:None, once=True)

        if timeout > 0:
            ts = time.time()
        else:
            ts = 0
        sent = False

        while not message.response_received:
            if not sent:
                self.send(path, message)
                sent = True
            if ts:
                if time.time() - ts > timeout:
                    raise exceptions.TimeoutError("send_and_wait(%s)"%path, timeout)

        return message.response_message
482731
def wait(self, duration=None, count=0):
        
        start = time.time()
        total = 0
        while True:
            type, result = self._recv(MSG, PING, OK)
            if type is MSG:
                total += 1
                if self._handle_msg(result) is False:
                    break

                if count and total >= count:
                    break

            elif type is PING:
                self._handle_ping()

            if duration and time.time() - start > duration:
                break
1239270
def RunOnce(self):
    
    # Attempt to fetch and load server certificate.
    if not self._FetchServerCertificate():
      self.timer.Wait()
      return HTTPObject(code=500)

    # Here we only drain messages if we were able to connect to the server in
    # the last poll request. Otherwise we just wait until the connection comes
    # back so we don't expire our messages too fast.
    if self.http_manager.consecutive_connection_errors == 0:
      # Grab some messages to send
      message_list = self.client_worker.Drain(
          max_size=config.CONFIG["Client.max_post_size"])
    else:
      message_list = rdf_flows.MessageList()

    # If any outbound messages require fast poll we switch to fast poll mode.
    for message in message_list.job:
      if message.require_fastpoll:
        self.timer.FastPoll()
        break

    # Make new encrypted ClientCommunication rdfvalue.
    payload = rdf_flows.ClientCommunication()

    # If our memory footprint is too large, we advertise that our input queue
    # is full. This will prevent the server from sending us any messages, and
    # hopefully allow us to work down our memory usage, by processing any
    # outstanding messages.
    if self.client_worker.MemoryExceeded():
      logging.info("Memory exceeded, will not retrieve jobs.")
      payload.queue_size = 1000000
    else:
      # Let the server know how many messages are currently queued in
      # the input queue.
      payload.queue_size = self.client_worker.InQueueSize()

    nonce = self.communicator.EncodeMessages(message_list, payload)
    payload_data = payload.SerializeToString()
    response = self.MakeRequest(payload_data)

    # Unable to decode response or response not valid.
    if response.code != 200 or response.messages is None:
      # We don't print response here since it should be encrypted and will
      # cause ascii conversion errors.
      logging.info("%s: Could not connect to server at %s, status %s",
                   self.communicator.common_name,
                   self.http_manager.active_base_url, response.code)

      # Force the server pem to be reparsed on the next connection.
      self.server_certificate = None

      # Reschedule the tasks back on the queue so they get retried next time.
      messages = list(message_list.job)
      for message in messages:
        message.require_fastpoll = False
        message.ttl -= 1
        if message.ttl > 0:
          self.client_worker.QueueResponse(message)
        else:
          logging.info("Dropped message due to retransmissions.")

      return response

    # Check the decoded nonce was as expected.
    if response.nonce != nonce:
      logging.info("Nonce not matched.")
      response.code = 500
      return response

    if response.source != self.communicator.server_name:
      logging.info("Received a message not from the server "
                   "%s, expected %s.", response.source,
                   self.communicator.server_name)
      response.code = 500
      return response

    # Check to see if any inbound messages want us to fastpoll. This means we
    # drop to fastpoll immediately on a new request rather than waiting for the
    # next beacon to report results.
    for message in response.messages:
      if message.require_fastpoll:
        self.timer.FastPoll()
        break

    # Process all messages. Messages can be processed by clients in
    # any order since clients do not have state.
    self.client_worker.QueueMessages(response.messages)

    cn = self.communicator.common_name
    logging.info(
        "%s: Sending %s(%s), Received %s messages in %s sec. "
        "Sleeping for %s sec.", cn, len(message_list), len(payload_data),
        len(response.messages), response.duration, self.timer.sleep_time)

    return response
122347
def _on_timeout():
    
    global _buffer_size

    LOGGER.debug('No metrics submitted in the last %.2f seconds',
                 _timeout_interval / 1000.0)
    _buffer_size = _pending_measurements()
    if _buffer_size:
        return _trigger_batch_write()
    _start_timeout()
847707
def send
      return if @queue.empty? || ENV['INSTANA_GEM_TEST']

      size = @queue.size
      if size > 200
        Instana.logger.debug "Trace queue is #{size}"
      end

      # Scan for any staged but incomplete traces that have now
      # completed.
      process_staged

      # Retrieve all spans for queued traces
      spans = queued_spans

      # Report spans in batches
      batch = spans.shift(@batch_size)
      while !batch.empty? do
        ::Instana.agent.report_spans(batch)
        batch = spans.shift(@batch_size)
      end
    end
568528
def _fetch_messages(self):
        
        try:
            [_, msg] = self.socket.recv_multipart(flags=zmq.NOBLOCK)
            if Global.CONFIG_MANAGER.tracing_mode:
                Global.LOGGER.debug("fetched a new message")

            self.fetched = self.fetched + 1
            obj = pickle.loads(msg)
            self._deliver_message(obj)
            return obj
        except zmq.error.Again:
            return None
        except Exception as new_exception:
            Global.LOGGER.error(new_exception)
            raise new_exception
730293
def recv_match(self, condition=None, type=None, blocking=False, timeout=None):
        ''''''
        if type is not None and not isinstance(type, list):
            type = [type]
        start_time = time.time()
        while True:
            if timeout is not None:
                now = time.time()
                if now < start_time:
                    start_time = now # If an external process rolls back system time, we should not spin forever.
                if start_time + timeout < time.time():
                    return None
            m = self.recv_msg()
            if m is None:
                if blocking:
                    for hook in self.idle_hooks:
                        hook(self)
                    if timeout is None:
                        self.select(0.05)
                    else:
                        self.select(timeout/2)
                    continue
                return None
            if type is not None and not m.get_type() in type:
                continue
            if not evaluate_condition(condition, self.messages):
                continue
            return m