Example #1
0
    def PushItem(self, item, block=True):
        """Push an item on to the queue.

    If no ZeroMQ socket has been created, one will be created the first time
    this method is called.

    Args:
      item (object): item to push on the queue.
      block (Optional[bool]): whether the push should be performed in blocking
          or non-block mode.

    Raises:
      QueueAlreadyClosed: If the queue is closed.
      QueueFull: If the internal buffer was full and it was not possible to
          push the item to the buffer within the timeout.
      RuntimeError: if closed event is missing.
    """
        if not self._closed_event:
            raise RuntimeError('Missing closed event.')

        if self._closed_event.is_set():
            raise errors.QueueAlreadyClosed()

        if not self._zmq_socket:
            self._CreateZMQSocket()

        try:
            if block:
                self._queue.put(item, timeout=self.timeout_seconds)
            else:
                self._queue.put(item, block=False)
        except Queue.Full as exception:
            raise errors.QueueFull(exception)
Example #2
0
    def Close(self, abort=False):
        """Closes the queue.

    Args:
      abort: If the Close is the result of an abort condition.

    Raises:
      QueueAlreadyClosed: If the queue is not started, or has already been
      closed.
    """
        if self._closed and not abort:
            raise errors.QueueAlreadyClosed()

        if abort:
            logging.warning(
                u'{0:s} queue aborting. Contents may be lost.'.format(
                    self.name))
            self._linger_seconds = 0
        else:
            logging.debug(
                u'{0:s} queue closing, will linger for up to {1:d} seconds'.
                format(self.name, self._linger_seconds))

        if not self._zmq_socket:
            return
        self._zmq_socket.close(self._linger_seconds)
Example #3
0
    def Close(self, abort=False):
        """Closes the queue.

    Args:
      abort (Optional[bool]): whether the Close is the result of an abort
          condition. If True, queue contents may be lost.

    Raises:
      QueueAlreadyClosed: If the queue is not started, or has already been
          closed.
      RuntimeError: if closed or terminate event is missing.
    """
        if not self._closed_event or not self._terminate_event:
            raise RuntimeError('Missing closed or terminate event.')

        if not abort and self._closed_event.is_set():
            raise errors.QueueAlreadyClosed()

        self._closed_event.set()

        if abort:
            if not self._closed_event.is_set():
                logger.warning(
                    '{0:s} queue aborting. Contents may be lost.'.format(
                        self.name))

            # We can't determine whether a there might be an operation being performed
            # on the socket in a separate method or thread, so we'll signal that any
            # such operation should cease.
            self._terminate_event.set()

            self._linger_seconds = 0

            if self._zmq_thread:
                logger.debug('[{0:s}] Waiting for thread to exit.'.format(
                    self.name))
                self._zmq_thread.join(timeout=self.timeout_seconds)
                if self._zmq_thread.isAlive():
                    logger.error((
                        '{0:s} ZMQ responder thread did not exit within timeout'
                    ).format(self.name))
        else:
            logger.debug(
                '{0:s} queue closing, will linger for up to {1:d} seconds'.
                format(self.name, self._linger_seconds))
Example #4
0
    def PushItem(self, item, block=True):
        """Push an item on to the queue.

    If no ZeroMQ socket has been created, one will be created the first time
    this method is called.

    Args:
      item: The item to push on to the queue.
      block: Optional argument to indicate whether the push should be performed
             in blocking or non-block mode.

    Raises:
      QueueAlreadyClosed: If there is an attempt to close a queue that's already
                          closed.
    """
        if self._closed:
            raise errors.QueueAlreadyClosed()
        if not self._zmq_socket:
            self._CreateZMQSocket()
        self._queue.put(item,
                        block=block,
                        timeout=self._buffer_timeout_seconds)