Skip to content

Blocking Connection Adapter

[!WARNING] BlockingConnection is deprecated and will be removed in Pika 2.0. Because the calling thread is the IOLoop, any blocking user code stalls heartbeats unless process_data_events() is called periodically, and the adapter is not thread-safe. Use Connection instead, which runs its own IOLoop on a background thread and provides a thread-safe blocking API.

blocking_connection

The blocking connection adapter module implements blocking semantics on top of Pika's core AMQP driver. While most of the asynchronous expectations are removed when using the blocking connection adapter, it attempts to remain true to the asynchronous RPC nature of the AMQP protocol, supporting server sent RPC commands.

The user facing classes in the module consist of the :py:class:~pika.adapters.blocking_connection.BlockingConnection and the :class:~pika.adapters.blocking_connection.BlockingChannel classes.

BlockingChannel

The BlockingChannel implements blocking semantics for most things that one would use callback-passing-style for with the :py:class:~pika.channel.Channel class. In addition, the BlockingChannel class implements a :term:generator that allows you to :doc:consume messages </examples/blocking_consumer_generator> without using callbacks.

Example of creating a BlockingChannel::

import pika

# Create our connection object
connection = pika.BlockingConnection()

# The returned object will be a synchronous channel
channel = connection.channel()
Source code in pika/adapters/blocking_connection.py
class BlockingChannel:
    """The BlockingChannel implements blocking semantics for most things that
    one would use callback-passing-style for with the
    :py:class:`~pika.channel.Channel` class. In addition,
    the `BlockingChannel` class implements a :term:`generator` that allows
    you to :doc:`consume messages </examples/blocking_consumer_generator>`
    without using callbacks.

    Example of creating a BlockingChannel::

        import pika

        # Create our connection object
        connection = pika.BlockingConnection()

        # The returned object will be a synchronous channel
        channel = connection.channel()

    """

    class _RxMessageArgs(NamedTuple):
        channel: Any
        method: Any
        properties: Any
        body: bytes

    class _MethodFrameCallbackResultArgs(NamedTuple):
        method_frame: Any

    class _OnMessageConfirmationReportArgs(NamedTuple):
        method_frame: Any

    class _FlowOkCallbackResultArgs(NamedTuple):
        active: bool

    _CONSUMER_CANCELLED_CB_KEY = 'blocking_channel_consumer_cancelled'

    def __init__(self, channel_impl: pika.channel.Channel,
                 connection: BlockingConnection) -> None:
        """
        Create a new instance of the Channel.

        :param channel_impl: Channel implementation object as returned from
            SelectConnection.channel()
        :param connection: The connection object
        """
        self._impl = channel_impl
        self._connection = connection

        # A mapping of consumer tags to _ConsumerInfo for active consumers
        self._consumer_infos: dict[Any, Any] = {}

        # Queue consumer generator generator info of type
        # _QueueConsumerGeneratorInfo created by BlockingChannel.consume
        self._queue_consumer_generator: _QueueConsumerGeneratorInfo | None = None

        # Whether RabbitMQ delivery confirmation has been enabled
        self._delivery_confirmation = False

        # Receives message delivery confirmation report (Basic.ack or
        # Basic.nack) from broker when delivery confirmations are enabled
        self._message_confirmation_result = _CallbackResult(
            self._OnMessageConfirmationReportArgs)

        # deque of pending events: _ConsumerDeliveryEvt and
        # _ConsumerCancellationEvt objects that will be returned by
        # `BlockingChannel.get_event()`
        self._pending_events: deque[Any] = deque()

        # Holds a ReturnedMessage object representing a message received via
        # Basic.Return in publisher-acknowledgments mode.
        self._puback_return: ReturnedMessage | None = None

        # self._on_channel_closed() saves the reason exception here
        self._closing_reason = None  # type: None | Exception

        # Receives Basic.ConsumeOk reply from server
        self._basic_consume_ok_result = _CallbackResult()

        # Receives args from Basic.GetEmpty response
        #  https://www.rabbitmq.com/amqp-0-9-1-reference.html#basic.get
        self._basic_getempty_result = _CallbackResult(
            self._MethodFrameCallbackResultArgs)

        self._impl.add_on_cancel_callback(self._on_consumer_cancelled_by_broker)

        self._impl.add_callback(self._basic_consume_ok_result.signal_once,
                                replies=[pika.spec.Basic.ConsumeOk],
                                one_shot=False)

        self._impl.add_on_close_callback(self._on_channel_closed)

        self._impl.add_callback(self._basic_getempty_result.set_value_once,
                                replies=[pika.spec.Basic.GetEmpty],
                                one_shot=False)

        LOGGER.info('Created channel=%s', self.channel_number)

    def __int__(self) -> int:
        """
        Return the channel object as its channel number.

        NOTE: inherited from legacy BlockingConnection; might be error-prone;
        use `channel_number` property instead.
        """
        return self.channel_number

    @override
    def __repr__(self) -> str:
        return f'<{self.__class__.__name__} impl={self._impl!r}>'

    def __enter__(self) -> Self:
        return self

    def __exit__(self, exc_type: type[BaseException] | None,
                 value: BaseException | None,
                 traceback: TracebackType | None) -> None:
        if self.is_open:
            self.close()

    def _cleanup(self) -> None:
        """Clean up members that might inhibit garbage collection."""
        self._message_confirmation_result.reset()
        self._pending_events = deque()
        self._consumer_infos = {}
        self._queue_consumer_generator = None

    @property
    def channel_number(self) -> int:
        """Channel number."""
        return self._impl.channel_number

    @property
    def connection(self) -> BlockingConnection:
        """The channel's BlockingConnection instance."""
        return self._connection

    @property
    def is_closed(self) -> bool:
        """Returns True if the channel is closed."""
        return self._impl.is_closed

    @property
    def is_open(self) -> bool:
        """Returns True if the channel is open."""
        return self._impl.is_open

    @property
    def consumer_tags(self) -> list[str]:
        """Property method that returns a list of consumer tags for active consumers."""
        return list(self._consumer_infos.keys())

    _ALWAYS_READY_WAITERS = ((lambda: True),)

    def _flush_output(self, *waiters: Callable[[], bool]) -> None:
        """
        Flush output and process input while waiting for any of the given callbacks to return true.
        The wait is aborted upon channel-close or connection-close. Otherwise, processing continues
        until the output is flushed AND at least one of the callbacks returns true. If there are no
        callbacks, then processing ends when all output is flushed.

        :param waiters: sequence of zero or more callables taking no args and returning true when
            it's time to stop processing. Their results are OR'ed together. An empty sequence is
            treated as equivalent to a waiter always returning true.
        """
        if self.is_closed:
            self._impl._raise_if_not_open()

        if not waiters:
            waiters = self._ALWAYS_READY_WAITERS

        self._connection._flush_output(lambda: self.is_closed, *waiters)

        if self.is_closed and isinstance(self._closing_reason,
                                         exceptions.ChannelClosedByBroker):
            raise self._closing_reason

    def _on_puback_message_returned(self, channel: pika.channel.Channel,
                                    method: pika.spec.Basic.Return,
                                    properties: pika.spec.BasicProperties,
                                    body: bytes) -> None:
        """
        Called as the result of Basic.Return from broker in publisher-acknowledgements mode.

        Saves the info as a ReturnedMessage instance in self._puback_return.

        :param channel: our self._impl channel
        :param method:
        :param properties: message properties
        :param body: returned message body; empty string if no body
        """
        assert channel is self._impl, (channel.channel_number,
                                       self.channel_number)

        assert isinstance(method, pika.spec.Basic.Return), method
        assert isinstance(properties, pika.spec.BasicProperties), (properties)

        LOGGER.warning(
            'Published message was returned: _delivery_confirmation=%s; '
            'channel=%s; method=%r; properties=%r; body_size=%d; '
            'body_prefix=%.255r', self._delivery_confirmation,
            channel.channel_number, method, properties,
            len(body) if body is not None else None, body)

        self._puback_return = ReturnedMessage(method, properties, body)

    def _add_pending_event(self, evt: _ChannelPendingEvt) -> None:
        """
        Append an event to the channel's list of events that are ready for dispatch to user and
        signal our connection that this channel is ready for event dispatch.

        :param evt: an event derived from _ChannelPendingEvt
        """
        self._pending_events.append(evt)
        self.connection._request_channel_dispatch(self.channel_number)

    def _on_channel_closed(self, _channel: pika.channel.Channel,
                           reason: Exception) -> None:
        """
        Callback from impl notifying us that the channel has been closed.

        This may be as the result of user-, broker-, or internal connection clean-up initiated closing or meta-closing of the
        channel.

        If it resulted from receiving `Channel.Close` from broker, we will
        expedite waking up of the event subsystem so that it may respond by
        raising `ChannelClosed` from user's context.

        NOTE: We can't raise exceptions in callbacks in order to protect
        the integrity of the underlying implementation. BlockingConnection's
        underlying asynchronous connection adapter (SelectConnection) uses
        callbacks to communicate with us. If BlockingConnection leaks exceptions
        back into the I/O loop or the asynchronous connection adapter, we
        interrupt their normal workflow and introduce a high likelihood of state
        inconsistency.

        See `pika.Channel.add_on_close_callback()` for additional documentation.

        :param _channel: (unused)
        :param reason:
        """
        LOGGER.debug('_on_channel_closed: %r; %r', reason, self)

        self._closing_reason = reason

        if isinstance(reason, exceptions.ChannelClosedByBroker):
            self.connection._server_channel_closures.append(reason)
            self._cleanup()

            # Request urgent termination of `process_data_events()`, in case
            # it's executing or next time it will execute
            self.connection._request_channel_dispatch(-self.channel_number)

    def _on_consumer_cancelled_by_broker(
            self,
            method_frame: pika.frame.Method[pika.spec.Basic.Cancel]) -> None:
        """
        Called by impl when broker cancels consumer via Basic.Cancel.

        This is a RabbitMQ-specific feature. The circumstances include deletion of queue being
        consumed as well as failure of a HA node responsible for the queue being consumed.

        :param method_frame: method frame with the `spec.Basic.Cancel` method
        """
        evt = _ConsumerCancellationEvt(method_frame)

        consumer = self._consumer_infos[method_frame.method.consumer_tag]

        # Don't interfere with client-initiated cancellation flow
        if not consumer.tearing_down:
            consumer.state = _ConsumerInfo.CANCELLED_BY_BROKER

        if consumer.alternate_event_sink is not None:
            consumer.alternate_event_sink(evt)
        else:
            self._add_pending_event(evt)

    def _on_consumer_message_delivery(self, _channel: pika.channel.Channel,
                                      method: pika.spec.Basic.Deliver,
                                      properties: pika.spec.BasicProperties,
                                      body: bytes) -> None:
        """
        Called by impl when a message is delivered for a consumer.

        :param _channel: The implementation channel object
        :param method:
        :param properties: message properties
        :param body: delivered message body; empty string if no body
        """
        evt = _ConsumerDeliveryEvt(method, properties, body)

        consumer = self._consumer_infos[method.consumer_tag]

        if consumer.alternate_event_sink is not None:
            consumer.alternate_event_sink(evt)
        else:
            self._add_pending_event(evt)

    def _on_consumer_generator_event(self, evt: _ChannelPendingEvt) -> None:
        """
        Sink for the queue consumer generator's consumer events; append the event to queue consumer
        generator's pending events buffer.

        :param evt: an object of type _ConsumerDeliveryEvt or _ConsumerCancellationEvt
        """
        assert self._queue_consumer_generator is not None
        self._queue_consumer_generator.pending_events.append(evt)
        # Schedule termination of connection.process_data_events using a
        # negative channel number
        self.connection._request_channel_dispatch(-self.channel_number)

    def _cancel_all_consumers(self) -> None:
        """
        Cancel all consumers.

        NOTE: pending non-ackable messages will be lost; pending ackable
        messages will be rejected.
        """
        if self._consumer_infos:
            LOGGER.debug('Cancelling %i consumers', len(self._consumer_infos))

            if self._queue_consumer_generator is not None:
                # Cancel queue consumer generator
                self.cancel()

            # Cancel consumers created via basic_consume
            for consumer_tag in list(self._consumer_infos.keys()):
                self.basic_cancel(consumer_tag)

    def _dispatch_events(self) -> None:
        """
        Called by BlockingConnection to dispatch pending events.

        `BlockingChannel` schedules this callback via `BlockingConnection._request_channel_dispatch`
        """
        while self._pending_events:
            evt = self._pending_events.popleft()

            if type(evt) is _ConsumerDeliveryEvt:
                consumer_info = self._consumer_infos[evt.method.consumer_tag]
                consumer_info.on_message_callback(self, evt.method,
                                                  evt.properties, evt.body)

            elif type(evt) is _ConsumerCancellationEvt:
                del self._consumer_infos[evt.method_frame.method.consumer_tag]

                self._impl.callbacks.process(self.channel_number,
                                             self._CONSUMER_CANCELLED_CB_KEY,
                                             self, evt.method_frame)
            else:
                evt.dispatch()

    def close(self,
              reply_code: int = 0,
              reply_text: str = 'Normal shutdown') -> None:
        """
        Will invoke a clean shutdown of the channel with the AMQP Broker.

        :param reply_code: The reply code to close the channel with
        :param reply_text: The reply text to close the channel with
        """
        LOGGER.debug('Channel.close(%s, %s)', reply_code, reply_text)

        self._impl._raise_if_not_open()

        try:
            # Cancel remaining consumers
            self._cancel_all_consumers()

            # Close the channel
            self._impl.close(reply_code=reply_code, reply_text=reply_text)
            self._flush_output(lambda: self.is_closed)
        finally:
            self._cleanup()

    def flow(self, active: bool) -> bool:
        """
        Turn Channel flow control off and on.

        NOTE: RabbitMQ doesn't support active=False; per
        https://www.rabbitmq.com/specification.html: "active=false is not
        supported by the server. Limiting prefetch with basic.qos provides much
        better control"

        For more information, please reference:

        https://www.rabbitmq.com/amqp-0-9-1-reference.html#channel.flow

        :param active: Turn flow on (True) or off (False)
        :returns: True if broker will start or continue sending; False if not
        """
        with _CallbackResult(self._FlowOkCallbackResultArgs) as flow_ok_result:
            self._impl.flow(active=active,
                            callback=flow_ok_result.set_value_once)
            self._flush_output(flow_ok_result.is_ready)
            return flow_ok_result.value.active

    def add_on_cancel_callback(
        self, callback: Callable[[pika.frame.Method[pika.spec.Basic.Cancel]],
                                 None]
    ) -> None:
        """
        Pass a callback function that will be called when Basic.Cancel is sent by the broker.

        The callback function should receive a method frame parameter.

        :param callback: a callable for handling broker's Basic.Cancel
            notification with the call signature: callback(method_frame)
            where method_frame is of type `pika.frame.Method` with method of
            type `spec.Basic.Cancel`
        """
        self._impl.callbacks.add(self.channel_number,
                                 self._CONSUMER_CANCELLED_CB_KEY,
                                 callback,
                                 one_shot=False)

    def add_on_return_callback(
        self, callback: Callable[[
            BlockingChannel, pika.spec.Basic.Return, pika.spec.
            BasicProperties, bytes
        ], None]
    ) -> None:
        """
        Pass a callback function that will be called when a published message is rejected and
        returned by the server via `Basic.Return`.

        :param callback: The method to call on callback with the
            signature callback(channel, method, properties, body), where
            - channel: pika.Channel
            - method: pika.spec.Basic.Return
            - properties: pika.spec.BasicProperties
            - body: bytes
        """
        self._impl.add_on_return_callback(
            lambda _channel, method, properties, body: (self._add_pending_event(
                _ReturnedMessageEvt(callback, self, method, properties, body))))

    def basic_consume(self,
                      queue: str,
                      on_message_callback: Callable[[
                          BlockingChannel, pika.spec.Basic.Deliver, pika.spec.
                          BasicProperties, bytes
                      ], None],
                      auto_ack: bool = False,
                      exclusive: bool = False,
                      consumer_tag: str | None = None,
                      arguments: dict[str, Any] | None = None) -> str:
        """
        Sends the AMQP command Basic.Consume to the broker and binds messages for the consumer_tag
        to the consumer callback. If you do not pass in a consumer_tag, one will be automatically
        generated for you. Returns the consumer tag.

        NOTE: the consumer callbacks are dispatched only in the scope of
        specially-designated methods: see
        `BlockingConnection.process_data_events` and
        `BlockingChannel.start_consuming`.

        For more information about Basic.Consume, see:
        https://www.rabbitmq.com/amqp-0-9-1-reference.html#basic.consume

        :param queue: The queue from which to consume
        :param on_message_callback: Required function for dispatching messages
            to user, having the signature:
            on_message_callback(channel, method, properties, body)
            - channel: BlockingChannel
            - method: spec.Basic.Deliver
            - properties: spec.BasicProperties
            - body: bytes
        :param auto_ack: if set to True, automatic acknowledgement mode will be used
                              (see https://www.rabbitmq.com/confirms.html). This corresponds
                              with the 'no_ack' parameter in the basic.consume AMQP 0.9.1
                              method
        :param exclusive: Don't allow other consumers on the queue
        :param consumer_tag: You may specify your own consumer tag; if left
          empty, a consumer tag will be generated automatically
        :param arguments: Custom key/value pair arguments for the consumer
        :returns: consumer tag
        :raises pika.exceptions.DuplicateConsumerTag: if consumer with given
            consumer_tag is already present.
        """
        validators.require_string(queue, 'queue')
        validators.require_callback(on_message_callback, 'on_message_callback')
        return self._basic_consume_impl(queue=queue,
                                        on_message_callback=on_message_callback,
                                        auto_ack=auto_ack,
                                        exclusive=exclusive,
                                        consumer_tag=consumer_tag,
                                        arguments=arguments)

    def _basic_consume_impl(
        self,
        queue: str,
        auto_ack: bool,
        exclusive: bool,
        consumer_tag: str | None,
        arguments: dict[str, Any] | None = None,
        on_message_callback: Callable[[
            BlockingChannel, pika.spec.Basic.Deliver, pika.spec.
            BasicProperties, bytes
        ], None] | None = None,
        alternate_event_sink: Callable[[_ChannelPendingEvt], None] | None = None
    ) -> str:
        """
        The low-level implementation used by `basic_consume` and `consume`.

        See `basic_consume` docstring for more info.

        NOTE: exactly one of on_message_callback/alternate_event_sink musts be
        non-None.

        This method has one additional parameter alternate_event_sink over the
        args described in `basic_consume`.

        :param queue: Queue name
        :param auto_ack: If True, the broker acknowledges messages automatically
        :param exclusive: If True, restrict access to the current connection
        :param consumer_tag: if specified, only the consumer with the given tag will be cancelled
        :param arguments: Custom key/value pair arguments for the consumer
        :param on_message_callback: Callback for delivered messages
        :param alternate_event_sink: if specified, _ConsumerDeliveryEvt
            and _ConsumerCancellationEvt objects will be diverted to this
            callback instead of being deposited in the channel's
            `_pending_events` container. Signature:
            alternate_event_sink(evt)

        :raises pika.exceptions.DuplicateConsumerTag: if consumer with given
            consumer_tag is already present.
        """
        if (on_message_callback is None) == (alternate_event_sink is None):
            raise ValueError(((
                'exactly one of on_message_callback/alternate_event_sink must '
                'be non-None'), on_message_callback, alternate_event_sink))

        if not consumer_tag:
            # Need a consumer tag to register consumer info before sending
            # request to broker, because I/O might dispatch incoming messages
            # immediately following Basic.Consume-ok before _flush_output
            # returns
            consumer_tag = self._impl._generate_consumer_tag()

        if consumer_tag in self._consumer_infos:
            raise exceptions.DuplicateConsumerTag(consumer_tag)

        # Create new consumer
        self._consumer_infos[consumer_tag] = _ConsumerInfo(
            consumer_tag,
            auto_ack=auto_ack,
            on_message_callback=on_message_callback,
            alternate_event_sink=alternate_event_sink)

        try:
            with self._basic_consume_ok_result as ok_result:
                tag = self._impl.basic_consume(
                    on_message_callback=self._on_consumer_message_delivery,
                    queue=queue,
                    auto_ack=auto_ack,
                    exclusive=exclusive,
                    consumer_tag=consumer_tag,
                    arguments=arguments)

                assert tag == consumer_tag, (tag, consumer_tag)

                self._flush_output(ok_result.is_ready)
        except Exception:
            # If channel was closed, self._consumer_infos will be empty
            if consumer_tag in self._consumer_infos:
                del self._consumer_infos[consumer_tag]
                # Schedule termination of connection.process_data_events using a
                # negative channel number
                self.connection._request_channel_dispatch(-self.channel_number)
            raise

        # NOTE: Consumer could get cancelled by broker immediately after opening
        # (e.g., queue getting deleted externally)
        if self._consumer_infos[consumer_tag].setting_up:
            self._consumer_infos[consumer_tag].state = _ConsumerInfo.ACTIVE

        return consumer_tag

    def basic_cancel(
        self, consumer_tag: str
    ) -> Sequence[tuple[pika.spec.Basic.Deliver, pika.spec.BasicProperties,
                        bytes]]:
        """
        This method cancels a consumer.

        This does not affect already delivered messages, but it does mean the server will not send any more
        messages for that consumer. The client may receive an arbitrary number
        of messages in between sending the cancel method and receiving the
        cancel-ok reply.

        NOTE: When cancelling an auto_ack=False consumer, this implementation
        automatically Nacks and suppresses any incoming messages that have not
        yet been dispatched to the consumer's callback. However, when cancelling
        a auto_ack=True consumer, this method will return any pending messages
        that arrived before broker confirmed the cancellation.

        :param consumer_tag: Identifier for the consumer; the result of
            passing a consumer_tag that was created on another channel is
            undefined (bad things will happen)
        :returns: (NEW IN pika 0.10.0) empty sequence for a auto_ack=False
            consumer; for a auto_ack=True consumer, returns a (possibly empty)
            sequence of pending messages that arrived before broker confirmed
            the cancellation (this is done instead of via consumer's callback in
            order to prevent reentrancy/recursion. Each message is three-tuple:
            (method, properties, body)
            - method: spec.Basic.Deliver
            - properties: spec.BasicProperties
            - body: bytes
        """
        try:
            consumer_info = self._consumer_infos[consumer_tag]
        except KeyError:
            LOGGER.warning(
                'User is attempting to cancel an unknown consumer=%s; '
                'already cancelled by user or broker?', consumer_tag)
            return []

        try:
            # Assertion failure here is most likely due to reentrance
            assert consumer_info.active or consumer_info.cancelled_by_broker, (
                consumer_info.state)

            # Assertion failure here signals disconnect between consumer state
            # in BlockingChannel and Channel
            assert (consumer_info.cancelled_by_broker or
                    consumer_tag in self._impl._consumers), consumer_tag

            auto_ack = consumer_info.auto_ack

            consumer_info.state = _ConsumerInfo.TEARING_DOWN

            with _CallbackResult() as cancel_ok_result:
                # Nack pending messages for auto_ack=False consumer
                if not auto_ack:
                    pending_messages = self._remove_pending_deliveries(
                        consumer_tag)
                    if pending_messages:
                        # NOTE: we use impl's basic_reject to avoid the
                        # possibility of redelivery before basic_cancel takes
                        # control of nacking.
                        # NOTE: we can't use basic_nack with the multiple option
                        # to avoid nacking messages already held by our client.
                        for message in pending_messages:
                            delivery_tag = message.method.delivery_tag
                            # The generated spec types delivery_tag as
                            # optional, but the broker always sets it on a
                            # delivery.
                            assert delivery_tag is not None
                            self._impl.basic_reject(delivery_tag, requeue=True)

                # Cancel the consumer; impl takes care of rejecting any
                # additional deliveries that arrive for a auto_ack=False
                # consumer
                self._impl.basic_cancel(consumer_tag=consumer_tag,
                                        callback=cancel_ok_result.signal_once)

                # Flush output and wait for Basic.Cancel-ok or
                # broker-initiated Basic.Cancel
                self._flush_output(
                    cancel_ok_result.is_ready,
                    lambda: consumer_tag not in self._impl._consumers)

            if auto_ack:
                # Return pending messages for auto_ack=True consumer
                return [(evt.method, evt.properties, evt.body)
                        for evt in self._remove_pending_deliveries(consumer_tag)
                       ]
            # impl takes care of rejecting any incoming deliveries during
            # cancellation
            messages = self._remove_pending_deliveries(consumer_tag)
            assert not messages, messages

            return []
        finally:
            # NOTE: The entry could be purged if channel or connection closes
            if consumer_tag in self._consumer_infos:
                del self._consumer_infos[consumer_tag]
                # Schedule termination of connection.process_data_events using a
                # negative channel number
                self.connection._request_channel_dispatch(-self.channel_number)

    def _remove_pending_deliveries(
            self, consumer_tag: str) -> Sequence[_ConsumerDeliveryEvt]:
        """
        Extract _ConsumerDeliveryEvt objects destined for the given consumer from pending events,
        discarding the _ConsumerCancellationEvt, if any.

        :param consumer_tag:

        :returns: a (possibly empty) sequence of _ConsumerDeliveryEvt destined for the given
            consumer tag
        """
        remaining_events: deque[Any] = deque()
        unprocessed_messages: list[Any] = []
        while self._pending_events:
            evt = self._pending_events.popleft()
            if (type(evt) is _ConsumerDeliveryEvt and
                    evt.method.consumer_tag == consumer_tag):
                unprocessed_messages.append(evt)
                continue
            if (type(evt) is _ConsumerCancellationEvt and
                    evt.method_frame.method.consumer_tag == consumer_tag):
                # A broker-initiated Basic.Cancel must have arrived
                # before our cancel request completed
                continue

            remaining_events.append(evt)

        self._pending_events = remaining_events

        return unprocessed_messages

    def start_consuming(self) -> None:
        """
        Processes I/O events and dispatches timers and `basic_consume` callbacks until all consumers
        are cancelled.

        NOTE: this blocking function may not be called from the scope of a
        pika callback, because dispatching `basic_consume` callbacks from this
        context would constitute recursion.

        :raises pika.exceptions.ReentrancyError: if called from the scope of a
            `BlockingConnection` or `BlockingChannel` callback
        :raises ChannelClosed: when this channel is closed by broker.
        """
        # Check if called from the scope of an event dispatch callback
        with self.connection._acquire_event_dispatch() as dispatch_allowed:
            if not dispatch_allowed:
                raise exceptions.ReentrancyError(
                    'start_consuming may not be called from the scope of '
                    'another BlockingConnection or BlockingChannel callback')

        self._impl._raise_if_not_open()

        # Process events as long as consumers exist on this channel
        while self._consumer_infos:
            # This will raise ChannelClosed if channel is closed by broker
            self._process_data_events(time_limit=None)

    def stop_consuming(self, consumer_tag: str | None = None) -> None:
        """
        Cancels all consumers, signalling the `start_consuming` loop to exit.

        NOTE: pending non-ackable messages will be lost; pending ackable
        messages will be rejected.

        :param consumer_tag: if specified, only the consumer with the given tag will be cancelled
        """
        if consumer_tag:
            self.basic_cancel(consumer_tag)
        else:
            self._cancel_all_consumers()

    def consume(
        self,
        queue: str,
        auto_ack: bool = False,
        exclusive: bool = False,
        arguments: dict[str, Any] | None = None,
        inactivity_timeout: float | None = None,
        consumer_tag: str | None = None
    ) -> Generator[tuple[pika.spec.Basic.Deliver | None,
                         pika.spec.BasicProperties | None, bytes | None]]:
        """
        Blocking consumption of a queue instead of via a callback.

        This method is a generator that yields each message as a tuple of method,
        properties, and body. The active generator iterator terminates when the
        consumer is cancelled by client via `BlockingChannel.cancel()` or by
        broker.

        Example:
        ::
            for method, properties, body in channel.consume('queue'):
                print(body)
                channel.basic_ack(method.delivery_tag)

        You should call `BlockingChannel.cancel()` when you escape out of the
        generator loop.

        If you don't cancel this consumer, then next call on the same channel
        to `consume()` with the exact same (queue, auto_ack, exclusive) parameters
        will resume the existing consumer generator; however, calling with
        different parameters will result in an exception.

        :param queue: The queue name to consume
        :param auto_ack: Tell the broker to not expect a ack/nack response
        :param exclusive: Don't allow other consumers on the queue
        :param arguments: Custom key/value pair arguments for the consumer
        :param inactivity_timeout: if a number is given (in
            seconds), will cause the method to yield (None, None, None) after the
            given period of inactivity; this permits for pseudo-regular maintenance
            activities to be carried out by the user while waiting for messages
            to arrive. If None is given (default), then the method blocks until
            the next event arrives. NOTE that timing granularity is limited by
            the timer resolution of the underlying implementation.
            NEW in pika 0.10.0.
        :param consumer_tag: Specify your own consumer tag

        :yields: tuple(spec.Basic.Deliver, spec.BasicProperties, bytes)

        :raises ValueError: if consumer-creation parameters don't match those
            of the existing queue consumer generator, if any.
            NEW in pika 0.10.0
        :raises ChannelClosed: when this channel is closed by broker.
        """
        self._impl._raise_if_not_open()

        params = (queue, auto_ack, exclusive)

        if self._queue_consumer_generator is not None:
            if params != self._queue_consumer_generator.params:
                raise ValueError(
                    'Consume with different params not allowed on existing '
                    f'queue consumer generator; previous params: {self._queue_consumer_generator.params!r}; '
                    f'new params: {(queue, auto_ack, exclusive)!r}')
        else:
            LOGGER.debug('Creating new queue consumer generator; params: %r',
                         params)
            # Need a consumer tag to register consumer info before sending
            # request to broker, because I/O might pick up incoming messages
            # in addition to Basic.Consume-ok
            if not consumer_tag:
                consumer_tag = self._impl._generate_consumer_tag()

            self._queue_consumer_generator = _QueueConsumerGeneratorInfo(
                params, consumer_tag)

            try:
                self._basic_consume_impl(
                    queue=queue,
                    auto_ack=auto_ack,
                    exclusive=exclusive,
                    consumer_tag=consumer_tag,
                    arguments=arguments,
                    alternate_event_sink=self._on_consumer_generator_event)
            except Exception:
                self._queue_consumer_generator = None
                raise

            LOGGER.info('Created new queue consumer generator %r',
                        self._queue_consumer_generator)

        while self._queue_consumer_generator is not None:
            # Process pending events
            if self._queue_consumer_generator.pending_events:
                evt = self._queue_consumer_generator.pending_events.popleft()
                if type(evt) is _ConsumerCancellationEvt:
                    # Consumer was cancelled by broker
                    self._queue_consumer_generator = None
                    break
                else:
                    yield (evt.method, evt.properties, evt.body)
                    continue

            if inactivity_timeout is None:
                # Wait indefinitely for a message to arrive, while processing
                # I/O events and triggering ChannelClosed exception when the
                # channel fails
                self._process_data_events(time_limit=None)
                continue

            # Wait with inactivity timeout
            wait_start_time = pika._utils.time_now()
            wait_deadline = wait_start_time + inactivity_timeout
            delta = inactivity_timeout

            while (self._queue_consumer_generator is not None and
                   not self._queue_consumer_generator.pending_events):

                self._process_data_events(time_limit=delta)

                if not self._queue_consumer_generator:
                    # Consumer was cancelled by client
                    break

                if self._queue_consumer_generator.pending_events:
                    # Got message(s)
                    break

                delta = wait_deadline - pika._utils.time_now()
                if delta <= 0.0:
                    # Signal inactivity timeout
                    yield (None, None, None)
                    break

    def _process_data_events(self, time_limit: float | None) -> None:
        """
        Wrapper for `BlockingConnection.process_data_events()` with common channel-specific logic
        that raises ChannelClosed if broker closed this channel.

        NOTE: We need to raise an exception in the context of user's call into
        our API to protect the integrity of the underlying implementation.
        BlockingConnection's underlying asynchronous connection adapter
        (SelectConnection) uses callbacks to communicate with us. If
        BlockingConnection leaks exceptions back into the I/O loop or the
        asynchronous connection adapter, we interrupt their normal workflow and
        introduce a high likelihood of state inconsistency.

        See `BlockingConnection.process_data_events()` for documentation of args
        and behavior.

        :param time_limit:
        """
        self.connection.process_data_events(time_limit=time_limit)
        if self.is_closed and isinstance(self._closing_reason,
                                         exceptions.ChannelClosedByBroker):
            LOGGER.debug('Channel close by broker detected, raising %r; %r',
                         self._closing_reason, self)
            raise self._closing_reason

    def get_waiting_message_count(self) -> int:
        """
        Returns the number of messages that may be retrieved from the current queue consumer
        generator via `BlockingChannel.consume` without blocking. NEW in pika 0.10.0.

        :returns: The number of waiting messages
        """
        if self._queue_consumer_generator is not None:
            pending_events = self._queue_consumer_generator.pending_events
            count = len(pending_events)
            if count and type(pending_events[-1]) is _ConsumerCancellationEvt:
                count -= 1
        else:
            count = 0

        return count

    def cancel(self) -> int:
        """
        Cancel the queue consumer created by `BlockingChannel.consume`, rejecting all pending
        ackable messages.

        NOTE: If you're looking to cancel a consumer issued with
        BlockingChannel.basic_consume then you should call
        BlockingChannel.basic_cancel.

        :returns: The number of messages requeued by Basic.Nack.
            NEW in 0.10.0: returns 0
        """
        if self._queue_consumer_generator is None:
            LOGGER.warning('cancel: queue consumer generator is inactive '
                           '(already cancelled by client or broker?)')
            return 0

        try:
            _, auto_ack, _ = self._queue_consumer_generator.params
            if not auto_ack:
                # Reject messages held by queue consumer generator; NOTE: we
                # can't use basic_nack with the multiple option to avoid nacking
                # messages already held by our client.
                pending_events = self._queue_consumer_generator.pending_events
                # NOTE `get_waiting_message_count` adjusts for `Basic.Cancel`
                #      from the server at the end (if any)
                for _ in range(self.get_waiting_message_count()):
                    evt = pending_events.popleft()
                    self._impl.basic_reject(evt.method.delivery_tag,
                                            requeue=True)

            self.basic_cancel(self._queue_consumer_generator.consumer_tag)
        finally:
            self._queue_consumer_generator = None

        # Return 0 for compatibility with legacy implementation; the number of
        # nacked messages is not meaningful since only messages consumed with
        # auto_ack=False may be nacked, and those arriving after calling
        # basic_cancel will be rejected automatically by impl channel, so we'll
        # never know how many of those were nacked.
        return 0

    def basic_ack(self, delivery_tag: int = 0, multiple: bool = False) -> None:
        """
        Acknowledge one or more messages.

        When sent by the client, this method acknowledges one or more messages delivered via the
        Deliver or Get-Ok methods. When sent by server, this method acknowledges one or more
        messages published with the Publish method on a channel in confirm mode. The acknowledgement
        can be for a single message or a set of messages up to and including a specific message.

        :param delivery_tag: The server-assigned delivery tag
        :param multiple: If set to True, the delivery tag is treated as "up to and including", so
            that multiple messages can be acknowledged with a single method. If set to False, the
            delivery tag refers to a single message. If the multiple field is 1, and the delivery
            tag is zero, this indicates acknowledgement of all outstanding messages.
        """
        self._impl.basic_ack(delivery_tag=delivery_tag, multiple=multiple)
        self._flush_output()

    def basic_nack(self,
                   delivery_tag: int = 0,
                   multiple: bool = False,
                   requeue: bool = True) -> None:
        """
        This method allows a client to reject one or more incoming messages.

        It can be used to interrupt and cancel large incoming messages, or return untreatable
        messages to their original queue.

        :param delivery_tag: The server-assigned delivery tag
        :param multiple: If set to True, the delivery tag is treated as "up to and including", so
            that multiple messages can be acknowledged with a single method. If set to False, the
            delivery tag refers to a single message. If the multiple field is 1, and the delivery
            tag is zero, this indicates acknowledgement of all outstanding messages.
        :param requeue: If requeue is true, the server will attempt to requeue the message. If
            requeue is false or the requeue attempt fails the messages are discarded or dead-
            lettered.
        """
        self._impl.basic_nack(delivery_tag=delivery_tag,
                              multiple=multiple,
                              requeue=requeue)
        self._flush_output()

    def basic_get(
        self,
        queue: str,
        auto_ack: bool = False
    ) -> tuple[pika.spec.Basic.GetOk | None, pika.spec.BasicProperties | None,
               bytes | None]:
        """
        Get a single message from the AMQP broker.

        Returns a sequence with the method frame, message properties, and body.

        :param queue: Name of queue from which to get a message
        :param auto_ack: Tell the broker to not expect a reply
        :returns: a three-tuple; (None, None, None) if the queue was empty; otherwise (method,
            properties, body); NOTE: body may be None
        """
        assert not self._basic_getempty_result

        validators.require_string(queue, 'queue')

        with contextlib.ExitStack() as stack:
            get_ok_result = stack.enter_context(
                _CallbackResult(self._RxMessageArgs))
            stack.enter_context(self._basic_getempty_result)
            self._impl.basic_get(queue=queue,
                                 auto_ack=auto_ack,
                                 callback=get_ok_result.set_value_once)
            self._flush_output(get_ok_result.is_ready,
                               self._basic_getempty_result.is_ready)
            if get_ok_result:
                evt = get_ok_result.value
                return evt.method, evt.properties, evt.body
            assert self._basic_getempty_result, (
                'wait completed without GetOk and GetEmpty')
            return None, None, None

    def basic_publish(self,
                      exchange: str,
                      routing_key: str,
                      body: bytes,
                      properties: pika.spec.BasicProperties | None = None,
                      mandatory: bool = False) -> None:
        """
        Publish to the channel with the given exchange, routing key, and body.

        For more information on basic_publish and what the parameters do, see:

            https://www.rabbitmq.com/amqp-0-9-1-reference.html#basic.publish

        NOTE: mandatory may be enabled even without delivery
          confirmation, but in the absence of delivery confirmation the
          synchronous implementation has no way to know how long to wait for
          the Basic.Return.

        :param exchange: The exchange to publish to
        :param routing_key: The routing key to bind on
        :param body: The message body; empty string if no body
        :param properties: message properties
        :param mandatory: The mandatory flag

        :raises UnroutableError: raised when a message published in
            publisher-acknowledgments mode (see
            `BlockingChannel.confirm_delivery`) is returned via `Basic.Return`
            followed by `Basic.Ack`.
        :raises NackError: raised when a message published in
            publisher-acknowledgements mode is Nack'ed by the broker. See
            `BlockingChannel.confirm_delivery`.
        """
        if self._delivery_confirmation:
            # In publisher-acknowledgments mode
            with self._message_confirmation_result:
                self._impl.basic_publish(exchange=exchange,
                                         routing_key=routing_key,
                                         body=body,
                                         properties=properties,
                                         mandatory=mandatory)

                self._flush_output(self._message_confirmation_result.is_ready)
                conf_method = (
                    self._message_confirmation_result.value.method_frame.method)

                if isinstance(conf_method, pika.spec.Basic.Nack):
                    # Broker was unable to process the message due to internal
                    # error
                    LOGGER.warning(
                        "Message was Nack'ed by broker: nack=%r; channel=%s; "
                        "exchange=%s; routing_key=%s; mandatory=%r; ",
                        conf_method, self.channel_number, exchange, routing_key,
                        mandatory)
                    if self._puback_return is not None:
                        returned_messages: list[ReturnedMessage] = [
                            self._puback_return
                        ]
                        self._puback_return = None
                    else:
                        returned_messages = []
                    raise exceptions.NackError(returned_messages)

                assert isinstance(conf_method,
                                  pika.spec.Basic.Ack), (conf_method)

                if self._puback_return is not None:
                    # Unroutable message was returned
                    messages = [self._puback_return]
                    self._puback_return = None
                    raise exceptions.UnroutableError(messages)
        else:
            # In non-publisher-acknowledgments mode
            self._impl.basic_publish(exchange=exchange,
                                     routing_key=routing_key,
                                     body=body,
                                     properties=properties,
                                     mandatory=mandatory)
            self._flush_output()

    def basic_qos(self,
                  prefetch_size: int = 0,
                  prefetch_count: int = 0,
                  global_qos: bool = False) -> None:
        """
        Specify quality of service.

        This method requests a specific quality of service. The QoS can be specified for the current
        channel or for all channels on the connection. The client can request that messages be sent
        in advance so that when the client finishes processing a message, the following message is
        already held locally, rather than needing to be sent down the channel. Prefetching gives a
        performance improvement.

        :param prefetch_size: This field specifies the prefetch window size. The server will send a
            message in advance if it is equal to or smaller in size than the available prefetch size
            (and also falls into other prefetch limits). May be set to zero, meaning "no specific
            limit", although other prefetch limits may still apply. The prefetch-size is ignored if
            the no-ack option is set in the consumer.
        :param prefetch_count: Specifies a prefetch window in terms of whole messages. This field
            may be used in combination with the prefetch-size field; a message will only be sent in
            advance if both prefetch windows (and those at the channel and connection level) allow
            it. The prefetch-count is ignored if the no-ack option is set in the consumer.
        :param global_qos: Should the QoS apply to all channels on the connection.
        """
        with _CallbackResult() as qos_ok_result:
            self._impl.basic_qos(callback=qos_ok_result.signal_once,
                                 prefetch_size=prefetch_size,
                                 prefetch_count=prefetch_count,
                                 global_qos=global_qos)
            self._flush_output(qos_ok_result.is_ready)

    def basic_recover(self, requeue: bool = False) -> None:
        """
        This method asks the server to redeliver all unacknowledged messages on a specified channel.
        Zero or more messages may be redelivered. This method replaces the asynchronous Recover.

        :param requeue: If False, the message will be redelivered to the original recipient. If
            True, the server will attempt to requeue the message, potentially then delivering it to
            an alternative subscriber.
        """
        with _CallbackResult() as recover_ok_result:
            self._impl.basic_recover(requeue=requeue,
                                     callback=recover_ok_result.signal_once)
            self._flush_output(recover_ok_result.is_ready)

    def basic_reject(self, delivery_tag: int = 0, requeue: bool = True) -> None:
        """
        Reject an incoming message.

        This method allows a client to reject a message. It can be used to interrupt and cancel
        large incoming messages, or return untreatable messages to their original queue.

        :param delivery_tag: The server-assigned delivery tag
        :param requeue: If requeue is true, the server will attempt to requeue the message. If
            requeue is false or the requeue attempt fails the messages are discarded or dead-
            lettered.
        """
        self._impl.basic_reject(delivery_tag=delivery_tag, requeue=requeue)
        self._flush_output()

    def confirm_delivery(self) -> None:
        """
        Turn on RabbitMQ-proprietary Confirm mode in the channel.

        For more information see:
            https://www.rabbitmq.com/confirms.html
        """
        if self._delivery_confirmation:
            LOGGER.error(
                'confirm_delivery: confirmation was already enabled '
                'on channel=%s', self.channel_number)
            return

        with _CallbackResult() as select_ok_result:
            self._impl.confirm_delivery(
                ack_nack_callback=self._message_confirmation_result.
                set_value_once,
                callback=select_ok_result.signal_once)

            self._flush_output(select_ok_result.is_ready)

        self._delivery_confirmation = True

        # Unroutable messages returned after this point will be in the context
        # of publisher acknowledgments
        self._impl.add_on_return_callback(self._on_puback_message_returned)

    def exchange_declare(self,
                         exchange: str,
                         exchange_type: (ExchangeType |
                                         str) = ExchangeType.direct,
                         passive: bool = False,
                         durable: bool = False,
                         auto_delete: bool = False,
                         internal: bool = False,
                         arguments: dict[str, Any] | None = None) -> None:
        """
        This method creates an exchange if it does not already exist, and if the exchange exists,
        verifies that it is of the correct and expected class.

        If passive set, the server will reply with Declare-Ok if the exchange
        already exists with the same name, and raise an error if not and if the
        exchange does not already exist, the server MUST raise a channel
        exception with reply code 404 (not found).

        :param exchange: The exchange name consists of a non-empty sequence of
                          these characters: letters, digits, hyphen, underscore,
                          period, or colon.
        :param exchange_type: The exchange type to use
        :param passive: Perform a declare or just check to see if it exists
        :param durable: Survive a reboot of RabbitMQ
        :param auto_delete: Remove when no more queues are bound to it
        :param internal: Can only be published to by other exchanges
        :param arguments: Custom key/value pair arguments for the exchange
        :returns: Method frame from the Exchange.Declare-ok response
        """
        validators.require_string(exchange, 'exchange')
        with _CallbackResult(
                self._MethodFrameCallbackResultArgs) as declare_ok_result:
            self._impl.exchange_declare(
                exchange=exchange,
                exchange_type=exchange_type,
                passive=passive,
                durable=durable,
                auto_delete=auto_delete,
                internal=internal,
                arguments=arguments,
                callback=declare_ok_result.set_value_once)

            self._flush_output(declare_ok_result.is_ready)
            return declare_ok_result.value.method_frame

    def exchange_delete(self,
                        exchange: str | None = None,
                        if_unused: bool = False
                       ) -> pika.frame.Method[pika.spec.Exchange.DeleteOk]:
        """
        Delete the exchange.

        :param exchange: The exchange name
        :param if_unused: only delete if the exchange is unused
        :returns: Method frame from the Exchange.Delete-ok response
        """
        with _CallbackResult(
                self._MethodFrameCallbackResultArgs) as delete_ok_result:
            self._impl.exchange_delete(exchange=exchange,
                                       if_unused=if_unused,
                                       callback=delete_ok_result.set_value_once)

            self._flush_output(delete_ok_result.is_ready)
            return delete_ok_result.value.method_frame

    def exchange_bind(
        self,
        destination: str,
        source: str,
        routing_key: str = '',
        arguments: dict[str, Any] | None = None
    ) -> pika.frame.Method[pika.spec.Exchange.BindOk]:
        """
        Bind an exchange to another exchange.

        :param destination: The destination exchange to bind
        :param source: The source exchange to bind to
        :param routing_key: The routing key to bind on
        :param arguments: Custom key/value pair arguments for the binding
        :returns: Method frame from the Exchange.Bind-ok response
        """
        validators.require_string(destination, 'destination')
        validators.require_string(source, 'source')
        with _CallbackResult(
                self._MethodFrameCallbackResultArgs) as bind_ok_result:
            self._impl.exchange_bind(destination=destination,
                                     source=source,
                                     routing_key=routing_key,
                                     arguments=arguments,
                                     callback=bind_ok_result.set_value_once)

            self._flush_output(bind_ok_result.is_ready)
            return bind_ok_result.value.method_frame

    def exchange_unbind(
        self,
        destination: str | None = None,
        source: str | None = None,
        routing_key: str = '',
        arguments: dict[str, Any] | None = None
    ) -> pika.frame.Method[pika.spec.Exchange.UnbindOk]:
        """
        Unbind an exchange from another exchange.

        :param destination: The destination exchange to unbind
        :param source: The source exchange to unbind from
        :param routing_key: The routing key to unbind
        :param arguments: Custom key/value pair arguments for the binding
        :returns: Method frame from the Exchange.Unbind-ok response
        """
        with _CallbackResult(
                self._MethodFrameCallbackResultArgs) as unbind_ok_result:
            self._impl.exchange_unbind(destination=destination,
                                       source=source,
                                       routing_key=routing_key,
                                       arguments=arguments,
                                       callback=unbind_ok_result.set_value_once)

            self._flush_output(unbind_ok_result.is_ready)
            return unbind_ok_result.value.method_frame

    def queue_declare(
        self,
        queue: str,
        passive: bool = False,
        durable: bool = False,
        exclusive: bool = False,
        auto_delete: bool = False,
        arguments: dict[str, Any] | None = None
    ) -> pika.frame.Method[pika.spec.Queue.DeclareOk]:
        """
        Declare queue, create if needed.

        This method creates or checks a queue. When creating a new queue the client can specify various
        properties that control the durability of the queue and its contents,
        and the level of sharing for the queue.

        Use an empty string as the queue name for the broker to auto-generate
        one. Retrieve this auto-generated queue name from the returned
        `spec.Queue.DeclareOk` method frame.

        :param queue: The queue name; if empty string, the broker will
            create a unique queue name
        :param passive: Only check to see if the queue exists and raise
          `ChannelClosed` if it doesn't
        :param durable: Survive reboots of the broker
        :param exclusive: Only allow access by the current connection
        :param auto_delete: Delete after consumer cancels or disconnects
        :param arguments: Custom key/value arguments for the queue
        :returns: Method frame from the Queue.Declare-ok response
        """
        validators.require_string(queue, 'queue')
        with _CallbackResult(
                self._MethodFrameCallbackResultArgs) as declare_ok_result:
            self._impl.queue_declare(queue=queue,
                                     passive=passive,
                                     durable=durable,
                                     exclusive=exclusive,
                                     auto_delete=auto_delete,
                                     arguments=arguments,
                                     callback=declare_ok_result.set_value_once)

            self._flush_output(declare_ok_result.is_ready)
            return declare_ok_result.value.method_frame

    def queue_delete(self,
                     queue: str,
                     if_unused: bool = False,
                     if_empty: bool = False
                    ) -> pika.frame.Method[pika.spec.Queue.DeleteOk]:
        """
        Delete a queue from the broker.

        :param queue: The queue to delete
        :param if_unused: only delete if it's unused
        :param if_empty: only delete if the queue is empty
        :returns: Method frame from the Queue.Delete-ok response
        """
        with _CallbackResult(
                self._MethodFrameCallbackResultArgs) as delete_ok_result:
            self._impl.queue_delete(queue=queue,
                                    if_unused=if_unused,
                                    if_empty=if_empty,
                                    callback=delete_ok_result.set_value_once)

            self._flush_output(delete_ok_result.is_ready)
            return delete_ok_result.value.method_frame

    def queue_purge(self,
                    queue: str) -> pika.frame.Method[pika.spec.Queue.PurgeOk]:
        """
        Purge all of the messages from the specified queue.

        :param queue: The queue to purge
        :returns: Method frame from the Queue.Purge-ok response
        """
        with _CallbackResult(
                self._MethodFrameCallbackResultArgs) as purge_ok_result:
            self._impl.queue_purge(queue=queue,
                                   callback=purge_ok_result.set_value_once)
            self._flush_output(purge_ok_result.is_ready)
            return purge_ok_result.value.method_frame

    def queue_bind(
        self,
        queue: str,
        exchange: str,
        routing_key: str | None = None,
        arguments: dict[str, Any] | None = None
    ) -> pika.frame.Method[pika.spec.Queue.BindOk]:
        """
        Bind the queue to the specified exchange.

        :param queue: The queue to bind to the exchange
        :param exchange: The source exchange to bind to
        :param routing_key: The routing key to bind on
        :param arguments: Custom key/value pair arguments for the binding
        :returns: Method frame from the Queue.Bind-ok response
        """
        validators.require_string(queue, 'queue')
        validators.require_string(exchange, 'exchange')
        with _CallbackResult(
                self._MethodFrameCallbackResultArgs) as bind_ok_result:
            self._impl.queue_bind(queue=queue,
                                  exchange=exchange,
                                  routing_key=routing_key,
                                  arguments=arguments,
                                  callback=bind_ok_result.set_value_once)
            self._flush_output(bind_ok_result.is_ready)
            return bind_ok_result.value.method_frame

    def queue_unbind(
        self,
        queue: str,
        exchange: str,
        routing_key: str | None = None,
        arguments: dict[str, Any] | None = None
    ) -> pika.frame.Method[pika.spec.Queue.UnbindOk]:
        """
        Unbind a queue from an exchange.

        :param queue: The queue to unbind from the exchange
        :param exchange: The source exchange to bind from
        :param routing_key: The routing key to unbind
        :param arguments: Custom key/value pair arguments for the binding
        :returns: Method frame from the Queue.Unbind-ok response
        """
        with _CallbackResult(
                self._MethodFrameCallbackResultArgs) as unbind_ok_result:
            self._impl.queue_unbind(queue=queue,
                                    exchange=exchange,
                                    routing_key=routing_key,
                                    arguments=arguments,
                                    callback=unbind_ok_result.set_value_once)
            self._flush_output(unbind_ok_result.is_ready)
            return unbind_ok_result.value.method_frame

    def tx_select(self) -> pika.frame.Method[pika.spec.Tx.SelectOk]:
        """
        Select standard transaction mode.

        This method sets the channel to use standard transactions. The client must use this method
        at least once on a channel before using the Commit or Rollback methods.

        :returns: Method frame from the Tx.Select-ok response
        """
        with _CallbackResult(
                self._MethodFrameCallbackResultArgs) as select_ok_result:
            self._impl.tx_select(select_ok_result.set_value_once)

            self._flush_output(select_ok_result.is_ready)
            return select_ok_result.value.method_frame

    def tx_commit(self) -> pika.frame.Method[pika.spec.Tx.CommitOk]:
        """
        Commit a transaction.

        :returns: Method frame from the Tx.Commit-ok response
        """
        with _CallbackResult(
                self._MethodFrameCallbackResultArgs) as commit_ok_result:
            self._impl.tx_commit(commit_ok_result.set_value_once)

            self._flush_output(commit_ok_result.is_ready)
            return commit_ok_result.value.method_frame

    def tx_rollback(self) -> pika.frame.Method[pika.spec.Tx.RollbackOk]:
        """
        Rollback a transaction.

        :returns: Method frame from the Tx.Rollback-ok response
        """
        with _CallbackResult(
                self._MethodFrameCallbackResultArgs) as rollback_ok_result:
            self._impl.tx_rollback(rollback_ok_result.set_value_once)

            self._flush_output(rollback_ok_result.is_ready)
            return rollback_ok_result.value.method_frame

channel_number property

channel_number: int

Channel number.

connection property

connection: BlockingConnection

The channel's BlockingConnection instance.

consumer_tags property

consumer_tags: list[str]

Property method that returns a list of consumer tags for active consumers.

is_closed property

is_closed: bool

Returns True if the channel is closed.

is_open property

is_open: bool

Returns True if the channel is open.

__init__

__init__(
    channel_impl: Channel, connection: BlockingConnection
) -> None

Create a new instance of the Channel.

PARAMETER DESCRIPTION
channel_impl

Channel implementation object as returned from SelectConnection.channel()

TYPE: Channel

connection

The connection object

TYPE: BlockingConnection

Source code in pika/adapters/blocking_connection.py
def __init__(self, channel_impl: pika.channel.Channel,
             connection: BlockingConnection) -> None:
    """
    Create a new instance of the Channel.

    :param channel_impl: Channel implementation object as returned from
        SelectConnection.channel()
    :param connection: The connection object
    """
    self._impl = channel_impl
    self._connection = connection

    # A mapping of consumer tags to _ConsumerInfo for active consumers
    self._consumer_infos: dict[Any, Any] = {}

    # Queue consumer generator generator info of type
    # _QueueConsumerGeneratorInfo created by BlockingChannel.consume
    self._queue_consumer_generator: _QueueConsumerGeneratorInfo | None = None

    # Whether RabbitMQ delivery confirmation has been enabled
    self._delivery_confirmation = False

    # Receives message delivery confirmation report (Basic.ack or
    # Basic.nack) from broker when delivery confirmations are enabled
    self._message_confirmation_result = _CallbackResult(
        self._OnMessageConfirmationReportArgs)

    # deque of pending events: _ConsumerDeliveryEvt and
    # _ConsumerCancellationEvt objects that will be returned by
    # `BlockingChannel.get_event()`
    self._pending_events: deque[Any] = deque()

    # Holds a ReturnedMessage object representing a message received via
    # Basic.Return in publisher-acknowledgments mode.
    self._puback_return: ReturnedMessage | None = None

    # self._on_channel_closed() saves the reason exception here
    self._closing_reason = None  # type: None | Exception

    # Receives Basic.ConsumeOk reply from server
    self._basic_consume_ok_result = _CallbackResult()

    # Receives args from Basic.GetEmpty response
    #  https://www.rabbitmq.com/amqp-0-9-1-reference.html#basic.get
    self._basic_getempty_result = _CallbackResult(
        self._MethodFrameCallbackResultArgs)

    self._impl.add_on_cancel_callback(self._on_consumer_cancelled_by_broker)

    self._impl.add_callback(self._basic_consume_ok_result.signal_once,
                            replies=[pika.spec.Basic.ConsumeOk],
                            one_shot=False)

    self._impl.add_on_close_callback(self._on_channel_closed)

    self._impl.add_callback(self._basic_getempty_result.set_value_once,
                            replies=[pika.spec.Basic.GetEmpty],
                            one_shot=False)

    LOGGER.info('Created channel=%s', self.channel_number)

__int__

__int__() -> int

Return the channel object as its channel number.

NOTE: inherited from legacy BlockingConnection; might be error-prone; use channel_number property instead.

Source code in pika/adapters/blocking_connection.py
def __int__(self) -> int:
    """
    Return the channel object as its channel number.

    NOTE: inherited from legacy BlockingConnection; might be error-prone;
    use `channel_number` property instead.
    """
    return self.channel_number

add_on_cancel_callback

add_on_cancel_callback(
    callback: Callable[[Method[Cancel]], None],
) -> None

Pass a callback function that will be called when Basic.Cancel is sent by the broker.

The callback function should receive a method frame parameter.

PARAMETER DESCRIPTION
callback

a callable for handling broker's Basic.Cancel notification with the call signature: callback(method_frame) where method_frame is of type pika.frame.Method with method of type spec.Basic.Cancel

TYPE: Callable[[Method[Cancel]], None]

Source code in pika/adapters/blocking_connection.py
def add_on_cancel_callback(
    self, callback: Callable[[pika.frame.Method[pika.spec.Basic.Cancel]],
                             None]
) -> None:
    """
    Pass a callback function that will be called when Basic.Cancel is sent by the broker.

    The callback function should receive a method frame parameter.

    :param callback: a callable for handling broker's Basic.Cancel
        notification with the call signature: callback(method_frame)
        where method_frame is of type `pika.frame.Method` with method of
        type `spec.Basic.Cancel`
    """
    self._impl.callbacks.add(self.channel_number,
                             self._CONSUMER_CANCELLED_CB_KEY,
                             callback,
                             one_shot=False)

add_on_return_callback

add_on_return_callback(
    callback: Callable[
        [BlockingChannel, Return, BasicProperties, bytes],
        None,
    ],
) -> None

Pass a callback function that will be called when a published message is rejected and returned by the server via Basic.Return.

PARAMETER DESCRIPTION
callback

The method to call on callback with the signature callback(channel, method, properties, body), where - channel: pika.Channel - method: pika.spec.Basic.Return - properties: pika.spec.BasicProperties - body: bytes

TYPE: Callable[[BlockingChannel, Return, BasicProperties, bytes], None]

Source code in pika/adapters/blocking_connection.py
def add_on_return_callback(
    self, callback: Callable[[
        BlockingChannel, pika.spec.Basic.Return, pika.spec.
        BasicProperties, bytes
    ], None]
) -> None:
    """
    Pass a callback function that will be called when a published message is rejected and
    returned by the server via `Basic.Return`.

    :param callback: The method to call on callback with the
        signature callback(channel, method, properties, body), where
        - channel: pika.Channel
        - method: pika.spec.Basic.Return
        - properties: pika.spec.BasicProperties
        - body: bytes
    """
    self._impl.add_on_return_callback(
        lambda _channel, method, properties, body: (self._add_pending_event(
            _ReturnedMessageEvt(callback, self, method, properties, body))))

basic_ack

basic_ack(
    delivery_tag: int = 0, multiple: bool = False
) -> None

Acknowledge one or more messages.

When sent by the client, this method acknowledges one or more messages delivered via the Deliver or Get-Ok methods. When sent by server, this method acknowledges one or more messages published with the Publish method on a channel in confirm mode. The acknowledgement can be for a single message or a set of messages up to and including a specific message.

PARAMETER DESCRIPTION
delivery_tag

The server-assigned delivery tag

TYPE: int DEFAULT: 0

multiple

If set to True, the delivery tag is treated as "up to and including", so that multiple messages can be acknowledged with a single method. If set to False, the delivery tag refers to a single message. If the multiple field is 1, and the delivery tag is zero, this indicates acknowledgement of all outstanding messages.

TYPE: bool DEFAULT: False

Source code in pika/adapters/blocking_connection.py
def basic_ack(self, delivery_tag: int = 0, multiple: bool = False) -> None:
    """
    Acknowledge one or more messages.

    When sent by the client, this method acknowledges one or more messages delivered via the
    Deliver or Get-Ok methods. When sent by server, this method acknowledges one or more
    messages published with the Publish method on a channel in confirm mode. The acknowledgement
    can be for a single message or a set of messages up to and including a specific message.

    :param delivery_tag: The server-assigned delivery tag
    :param multiple: If set to True, the delivery tag is treated as "up to and including", so
        that multiple messages can be acknowledged with a single method. If set to False, the
        delivery tag refers to a single message. If the multiple field is 1, and the delivery
        tag is zero, this indicates acknowledgement of all outstanding messages.
    """
    self._impl.basic_ack(delivery_tag=delivery_tag, multiple=multiple)
    self._flush_output()

basic_cancel

basic_cancel(
    consumer_tag: str,
) -> Sequence[tuple[Deliver, BasicProperties, bytes]]

This method cancels a consumer.

This does not affect already delivered messages, but it does mean the server will not send any more messages for that consumer. The client may receive an arbitrary number of messages in between sending the cancel method and receiving the cancel-ok reply.

NOTE: When cancelling an auto_ack=False consumer, this implementation automatically Nacks and suppresses any incoming messages that have not yet been dispatched to the consumer's callback. However, when cancelling a auto_ack=True consumer, this method will return any pending messages that arrived before broker confirmed the cancellation.

PARAMETER DESCRIPTION
consumer_tag

Identifier for the consumer; the result of passing a consumer_tag that was created on another channel is undefined (bad things will happen)

TYPE: str

RETURNS DESCRIPTION
Sequence[tuple[Deliver, BasicProperties, bytes]]

(NEW IN pika 0.10.0) empty sequence for a auto_ack=False consumer; for a auto_ack=True consumer, returns a (possibly empty) sequence of pending messages that arrived before broker confirmed the cancellation (this is done instead of via consumer's callback in order to prevent reentrancy/recursion. Each message is three-tuple: (method, properties, body) - method: spec.Basic.Deliver - properties: spec.BasicProperties - body: bytes

Source code in pika/adapters/blocking_connection.py
def basic_cancel(
    self, consumer_tag: str
) -> Sequence[tuple[pika.spec.Basic.Deliver, pika.spec.BasicProperties,
                    bytes]]:
    """
    This method cancels a consumer.

    This does not affect already delivered messages, but it does mean the server will not send any more
    messages for that consumer. The client may receive an arbitrary number
    of messages in between sending the cancel method and receiving the
    cancel-ok reply.

    NOTE: When cancelling an auto_ack=False consumer, this implementation
    automatically Nacks and suppresses any incoming messages that have not
    yet been dispatched to the consumer's callback. However, when cancelling
    a auto_ack=True consumer, this method will return any pending messages
    that arrived before broker confirmed the cancellation.

    :param consumer_tag: Identifier for the consumer; the result of
        passing a consumer_tag that was created on another channel is
        undefined (bad things will happen)
    :returns: (NEW IN pika 0.10.0) empty sequence for a auto_ack=False
        consumer; for a auto_ack=True consumer, returns a (possibly empty)
        sequence of pending messages that arrived before broker confirmed
        the cancellation (this is done instead of via consumer's callback in
        order to prevent reentrancy/recursion. Each message is three-tuple:
        (method, properties, body)
        - method: spec.Basic.Deliver
        - properties: spec.BasicProperties
        - body: bytes
    """
    try:
        consumer_info = self._consumer_infos[consumer_tag]
    except KeyError:
        LOGGER.warning(
            'User is attempting to cancel an unknown consumer=%s; '
            'already cancelled by user or broker?', consumer_tag)
        return []

    try:
        # Assertion failure here is most likely due to reentrance
        assert consumer_info.active or consumer_info.cancelled_by_broker, (
            consumer_info.state)

        # Assertion failure here signals disconnect between consumer state
        # in BlockingChannel and Channel
        assert (consumer_info.cancelled_by_broker or
                consumer_tag in self._impl._consumers), consumer_tag

        auto_ack = consumer_info.auto_ack

        consumer_info.state = _ConsumerInfo.TEARING_DOWN

        with _CallbackResult() as cancel_ok_result:
            # Nack pending messages for auto_ack=False consumer
            if not auto_ack:
                pending_messages = self._remove_pending_deliveries(
                    consumer_tag)
                if pending_messages:
                    # NOTE: we use impl's basic_reject to avoid the
                    # possibility of redelivery before basic_cancel takes
                    # control of nacking.
                    # NOTE: we can't use basic_nack with the multiple option
                    # to avoid nacking messages already held by our client.
                    for message in pending_messages:
                        delivery_tag = message.method.delivery_tag
                        # The generated spec types delivery_tag as
                        # optional, but the broker always sets it on a
                        # delivery.
                        assert delivery_tag is not None
                        self._impl.basic_reject(delivery_tag, requeue=True)

            # Cancel the consumer; impl takes care of rejecting any
            # additional deliveries that arrive for a auto_ack=False
            # consumer
            self._impl.basic_cancel(consumer_tag=consumer_tag,
                                    callback=cancel_ok_result.signal_once)

            # Flush output and wait for Basic.Cancel-ok or
            # broker-initiated Basic.Cancel
            self._flush_output(
                cancel_ok_result.is_ready,
                lambda: consumer_tag not in self._impl._consumers)

        if auto_ack:
            # Return pending messages for auto_ack=True consumer
            return [(evt.method, evt.properties, evt.body)
                    for evt in self._remove_pending_deliveries(consumer_tag)
                   ]
        # impl takes care of rejecting any incoming deliveries during
        # cancellation
        messages = self._remove_pending_deliveries(consumer_tag)
        assert not messages, messages

        return []
    finally:
        # NOTE: The entry could be purged if channel or connection closes
        if consumer_tag in self._consumer_infos:
            del self._consumer_infos[consumer_tag]
            # Schedule termination of connection.process_data_events using a
            # negative channel number
            self.connection._request_channel_dispatch(-self.channel_number)

basic_consume

basic_consume(
    queue: str,
    on_message_callback: Callable[
        [BlockingChannel, Deliver, BasicProperties, bytes],
        None,
    ],
    auto_ack: bool = False,
    exclusive: bool = False,
    consumer_tag: str | None = None,
    arguments: dict[str, Any] | None = None,
) -> str

Sends the AMQP command Basic.Consume to the broker and binds messages for the consumer_tag to the consumer callback. If you do not pass in a consumer_tag, one will be automatically generated for you. Returns the consumer tag.

NOTE: the consumer callbacks are dispatched only in the scope of specially-designated methods: see BlockingConnection.process_data_events and BlockingChannel.start_consuming.

For more information about Basic.Consume, see: https://www.rabbitmq.com/amqp-0-9-1-reference.html#basic.consume

PARAMETER DESCRIPTION
queue

The queue from which to consume

TYPE: str

on_message_callback

Required function for dispatching messages to user, having the signature: on_message_callback(channel, method, properties, body) - channel: BlockingChannel - method: spec.Basic.Deliver - properties: spec.BasicProperties - body: bytes

TYPE: Callable[[BlockingChannel, Deliver, BasicProperties, bytes], None]

auto_ack

if set to True, automatic acknowledgement mode will be used (see https://www.rabbitmq.com/confirms.html). This corresponds with the 'no_ack' parameter in the basic.consume AMQP 0.9.1 method

TYPE: bool DEFAULT: False

exclusive

Don't allow other consumers on the queue

TYPE: bool DEFAULT: False

consumer_tag

You may specify your own consumer tag; if left empty, a consumer tag will be generated automatically

TYPE: str | None DEFAULT: None

arguments

Custom key/value pair arguments for the consumer

TYPE: dict[str, Any] | None DEFAULT: None

RETURNS DESCRIPTION
str

consumer tag

RAISES DESCRIPTION
pika.exceptions.DuplicateConsumerTag

if consumer with given consumer_tag is already present.

Source code in pika/adapters/blocking_connection.py
def basic_consume(self,
                  queue: str,
                  on_message_callback: Callable[[
                      BlockingChannel, pika.spec.Basic.Deliver, pika.spec.
                      BasicProperties, bytes
                  ], None],
                  auto_ack: bool = False,
                  exclusive: bool = False,
                  consumer_tag: str | None = None,
                  arguments: dict[str, Any] | None = None) -> str:
    """
    Sends the AMQP command Basic.Consume to the broker and binds messages for the consumer_tag
    to the consumer callback. If you do not pass in a consumer_tag, one will be automatically
    generated for you. Returns the consumer tag.

    NOTE: the consumer callbacks are dispatched only in the scope of
    specially-designated methods: see
    `BlockingConnection.process_data_events` and
    `BlockingChannel.start_consuming`.

    For more information about Basic.Consume, see:
    https://www.rabbitmq.com/amqp-0-9-1-reference.html#basic.consume

    :param queue: The queue from which to consume
    :param on_message_callback: Required function for dispatching messages
        to user, having the signature:
        on_message_callback(channel, method, properties, body)
        - channel: BlockingChannel
        - method: spec.Basic.Deliver
        - properties: spec.BasicProperties
        - body: bytes
    :param auto_ack: if set to True, automatic acknowledgement mode will be used
                          (see https://www.rabbitmq.com/confirms.html). This corresponds
                          with the 'no_ack' parameter in the basic.consume AMQP 0.9.1
                          method
    :param exclusive: Don't allow other consumers on the queue
    :param consumer_tag: You may specify your own consumer tag; if left
      empty, a consumer tag will be generated automatically
    :param arguments: Custom key/value pair arguments for the consumer
    :returns: consumer tag
    :raises pika.exceptions.DuplicateConsumerTag: if consumer with given
        consumer_tag is already present.
    """
    validators.require_string(queue, 'queue')
    validators.require_callback(on_message_callback, 'on_message_callback')
    return self._basic_consume_impl(queue=queue,
                                    on_message_callback=on_message_callback,
                                    auto_ack=auto_ack,
                                    exclusive=exclusive,
                                    consumer_tag=consumer_tag,
                                    arguments=arguments)

basic_get

basic_get(
    queue: str, auto_ack: bool = False
) -> tuple[
    GetOk | None, BasicProperties | None, bytes | None
]

Get a single message from the AMQP broker.

Returns a sequence with the method frame, message properties, and body.

PARAMETER DESCRIPTION
queue

Name of queue from which to get a message

TYPE: str

auto_ack

Tell the broker to not expect a reply

TYPE: bool DEFAULT: False

RETURNS DESCRIPTION
tuple[GetOk | None, BasicProperties | None, bytes | None]

a three-tuple; (None, None, None) if the queue was empty; otherwise (method, properties, body); NOTE: body may be None

Source code in pika/adapters/blocking_connection.py
def basic_get(
    self,
    queue: str,
    auto_ack: bool = False
) -> tuple[pika.spec.Basic.GetOk | None, pika.spec.BasicProperties | None,
           bytes | None]:
    """
    Get a single message from the AMQP broker.

    Returns a sequence with the method frame, message properties, and body.

    :param queue: Name of queue from which to get a message
    :param auto_ack: Tell the broker to not expect a reply
    :returns: a three-tuple; (None, None, None) if the queue was empty; otherwise (method,
        properties, body); NOTE: body may be None
    """
    assert not self._basic_getempty_result

    validators.require_string(queue, 'queue')

    with contextlib.ExitStack() as stack:
        get_ok_result = stack.enter_context(
            _CallbackResult(self._RxMessageArgs))
        stack.enter_context(self._basic_getempty_result)
        self._impl.basic_get(queue=queue,
                             auto_ack=auto_ack,
                             callback=get_ok_result.set_value_once)
        self._flush_output(get_ok_result.is_ready,
                           self._basic_getempty_result.is_ready)
        if get_ok_result:
            evt = get_ok_result.value
            return evt.method, evt.properties, evt.body
        assert self._basic_getempty_result, (
            'wait completed without GetOk and GetEmpty')
        return None, None, None

basic_nack

basic_nack(
    delivery_tag: int = 0,
    multiple: bool = False,
    requeue: bool = True,
) -> None

This method allows a client to reject one or more incoming messages.

It can be used to interrupt and cancel large incoming messages, or return untreatable messages to their original queue.

PARAMETER DESCRIPTION
delivery_tag

The server-assigned delivery tag

TYPE: int DEFAULT: 0

multiple

If set to True, the delivery tag is treated as "up to and including", so that multiple messages can be acknowledged with a single method. If set to False, the delivery tag refers to a single message. If the multiple field is 1, and the delivery tag is zero, this indicates acknowledgement of all outstanding messages.

TYPE: bool DEFAULT: False

requeue

If requeue is true, the server will attempt to requeue the message. If requeue is false or the requeue attempt fails the messages are discarded or dead- lettered.

TYPE: bool DEFAULT: True

Source code in pika/adapters/blocking_connection.py
def basic_nack(self,
               delivery_tag: int = 0,
               multiple: bool = False,
               requeue: bool = True) -> None:
    """
    This method allows a client to reject one or more incoming messages.

    It can be used to interrupt and cancel large incoming messages, or return untreatable
    messages to their original queue.

    :param delivery_tag: The server-assigned delivery tag
    :param multiple: If set to True, the delivery tag is treated as "up to and including", so
        that multiple messages can be acknowledged with a single method. If set to False, the
        delivery tag refers to a single message. If the multiple field is 1, and the delivery
        tag is zero, this indicates acknowledgement of all outstanding messages.
    :param requeue: If requeue is true, the server will attempt to requeue the message. If
        requeue is false or the requeue attempt fails the messages are discarded or dead-
        lettered.
    """
    self._impl.basic_nack(delivery_tag=delivery_tag,
                          multiple=multiple,
                          requeue=requeue)
    self._flush_output()

basic_publish

basic_publish(
    exchange: str,
    routing_key: str,
    body: bytes,
    properties: BasicProperties | None = None,
    mandatory: bool = False,
) -> None

Publish to the channel with the given exchange, routing key, and body.

For more information on basic_publish and what the parameters do, see:

https://www.rabbitmq.com/amqp-0-9-1-reference.html#basic.publish

NOTE: mandatory may be enabled even without delivery confirmation, but in the absence of delivery confirmation the synchronous implementation has no way to know how long to wait for the Basic.Return.

PARAMETER DESCRIPTION
exchange

The exchange to publish to

TYPE: str

routing_key

The routing key to bind on

TYPE: str

body

The message body; empty string if no body

TYPE: bytes

properties

message properties

TYPE: BasicProperties | None DEFAULT: None

mandatory

The mandatory flag

TYPE: bool DEFAULT: False

RAISES DESCRIPTION
UnroutableError

raised when a message published in publisher-acknowledgments mode (see BlockingChannel.confirm_delivery) is returned via Basic.Return followed by Basic.Ack.

NackError

raised when a message published in publisher-acknowledgements mode is Nack'ed by the broker. See BlockingChannel.confirm_delivery.

Source code in pika/adapters/blocking_connection.py
def basic_publish(self,
                  exchange: str,
                  routing_key: str,
                  body: bytes,
                  properties: pika.spec.BasicProperties | None = None,
                  mandatory: bool = False) -> None:
    """
    Publish to the channel with the given exchange, routing key, and body.

    For more information on basic_publish and what the parameters do, see:

        https://www.rabbitmq.com/amqp-0-9-1-reference.html#basic.publish

    NOTE: mandatory may be enabled even without delivery
      confirmation, but in the absence of delivery confirmation the
      synchronous implementation has no way to know how long to wait for
      the Basic.Return.

    :param exchange: The exchange to publish to
    :param routing_key: The routing key to bind on
    :param body: The message body; empty string if no body
    :param properties: message properties
    :param mandatory: The mandatory flag

    :raises UnroutableError: raised when a message published in
        publisher-acknowledgments mode (see
        `BlockingChannel.confirm_delivery`) is returned via `Basic.Return`
        followed by `Basic.Ack`.
    :raises NackError: raised when a message published in
        publisher-acknowledgements mode is Nack'ed by the broker. See
        `BlockingChannel.confirm_delivery`.
    """
    if self._delivery_confirmation:
        # In publisher-acknowledgments mode
        with self._message_confirmation_result:
            self._impl.basic_publish(exchange=exchange,
                                     routing_key=routing_key,
                                     body=body,
                                     properties=properties,
                                     mandatory=mandatory)

            self._flush_output(self._message_confirmation_result.is_ready)
            conf_method = (
                self._message_confirmation_result.value.method_frame.method)

            if isinstance(conf_method, pika.spec.Basic.Nack):
                # Broker was unable to process the message due to internal
                # error
                LOGGER.warning(
                    "Message was Nack'ed by broker: nack=%r; channel=%s; "
                    "exchange=%s; routing_key=%s; mandatory=%r; ",
                    conf_method, self.channel_number, exchange, routing_key,
                    mandatory)
                if self._puback_return is not None:
                    returned_messages: list[ReturnedMessage] = [
                        self._puback_return
                    ]
                    self._puback_return = None
                else:
                    returned_messages = []
                raise exceptions.NackError(returned_messages)

            assert isinstance(conf_method,
                              pika.spec.Basic.Ack), (conf_method)

            if self._puback_return is not None:
                # Unroutable message was returned
                messages = [self._puback_return]
                self._puback_return = None
                raise exceptions.UnroutableError(messages)
    else:
        # In non-publisher-acknowledgments mode
        self._impl.basic_publish(exchange=exchange,
                                 routing_key=routing_key,
                                 body=body,
                                 properties=properties,
                                 mandatory=mandatory)
        self._flush_output()

basic_qos

basic_qos(
    prefetch_size: int = 0,
    prefetch_count: int = 0,
    global_qos: bool = False,
) -> None

Specify quality of service.

This method requests a specific quality of service. The QoS can be specified for the current channel or for all channels on the connection. The client can request that messages be sent in advance so that when the client finishes processing a message, the following message is already held locally, rather than needing to be sent down the channel. Prefetching gives a performance improvement.

PARAMETER DESCRIPTION
prefetch_size

This field specifies the prefetch window size. The server will send a message in advance if it is equal to or smaller in size than the available prefetch size (and also falls into other prefetch limits). May be set to zero, meaning "no specific limit", although other prefetch limits may still apply. The prefetch-size is ignored if the no-ack option is set in the consumer.

TYPE: int DEFAULT: 0

prefetch_count

Specifies a prefetch window in terms of whole messages. This field may be used in combination with the prefetch-size field; a message will only be sent in advance if both prefetch windows (and those at the channel and connection level) allow it. The prefetch-count is ignored if the no-ack option is set in the consumer.

TYPE: int DEFAULT: 0

global_qos

Should the QoS apply to all channels on the connection.

TYPE: bool DEFAULT: False

Source code in pika/adapters/blocking_connection.py
def basic_qos(self,
              prefetch_size: int = 0,
              prefetch_count: int = 0,
              global_qos: bool = False) -> None:
    """
    Specify quality of service.

    This method requests a specific quality of service. The QoS can be specified for the current
    channel or for all channels on the connection. The client can request that messages be sent
    in advance so that when the client finishes processing a message, the following message is
    already held locally, rather than needing to be sent down the channel. Prefetching gives a
    performance improvement.

    :param prefetch_size: This field specifies the prefetch window size. The server will send a
        message in advance if it is equal to or smaller in size than the available prefetch size
        (and also falls into other prefetch limits). May be set to zero, meaning "no specific
        limit", although other prefetch limits may still apply. The prefetch-size is ignored if
        the no-ack option is set in the consumer.
    :param prefetch_count: Specifies a prefetch window in terms of whole messages. This field
        may be used in combination with the prefetch-size field; a message will only be sent in
        advance if both prefetch windows (and those at the channel and connection level) allow
        it. The prefetch-count is ignored if the no-ack option is set in the consumer.
    :param global_qos: Should the QoS apply to all channels on the connection.
    """
    with _CallbackResult() as qos_ok_result:
        self._impl.basic_qos(callback=qos_ok_result.signal_once,
                             prefetch_size=prefetch_size,
                             prefetch_count=prefetch_count,
                             global_qos=global_qos)
        self._flush_output(qos_ok_result.is_ready)

basic_recover

basic_recover(requeue: bool = False) -> None

This method asks the server to redeliver all unacknowledged messages on a specified channel. Zero or more messages may be redelivered. This method replaces the asynchronous Recover.

PARAMETER DESCRIPTION
requeue

If False, the message will be redelivered to the original recipient. If True, the server will attempt to requeue the message, potentially then delivering it to an alternative subscriber.

TYPE: bool DEFAULT: False

Source code in pika/adapters/blocking_connection.py
def basic_recover(self, requeue: bool = False) -> None:
    """
    This method asks the server to redeliver all unacknowledged messages on a specified channel.
    Zero or more messages may be redelivered. This method replaces the asynchronous Recover.

    :param requeue: If False, the message will be redelivered to the original recipient. If
        True, the server will attempt to requeue the message, potentially then delivering it to
        an alternative subscriber.
    """
    with _CallbackResult() as recover_ok_result:
        self._impl.basic_recover(requeue=requeue,
                                 callback=recover_ok_result.signal_once)
        self._flush_output(recover_ok_result.is_ready)

basic_reject

basic_reject(
    delivery_tag: int = 0, requeue: bool = True
) -> None

Reject an incoming message.

This method allows a client to reject a message. It can be used to interrupt and cancel large incoming messages, or return untreatable messages to their original queue.

PARAMETER DESCRIPTION
delivery_tag

The server-assigned delivery tag

TYPE: int DEFAULT: 0

requeue

If requeue is true, the server will attempt to requeue the message. If requeue is false or the requeue attempt fails the messages are discarded or dead- lettered.

TYPE: bool DEFAULT: True

Source code in pika/adapters/blocking_connection.py
def basic_reject(self, delivery_tag: int = 0, requeue: bool = True) -> None:
    """
    Reject an incoming message.

    This method allows a client to reject a message. It can be used to interrupt and cancel
    large incoming messages, or return untreatable messages to their original queue.

    :param delivery_tag: The server-assigned delivery tag
    :param requeue: If requeue is true, the server will attempt to requeue the message. If
        requeue is false or the requeue attempt fails the messages are discarded or dead-
        lettered.
    """
    self._impl.basic_reject(delivery_tag=delivery_tag, requeue=requeue)
    self._flush_output()

cancel

cancel() -> int

Cancel the queue consumer created by BlockingChannel.consume, rejecting all pending ackable messages.

NOTE: If you're looking to cancel a consumer issued with BlockingChannel.basic_consume then you should call BlockingChannel.basic_cancel.

RETURNS DESCRIPTION
int

The number of messages requeued by Basic.Nack. NEW in 0.10.0: returns 0

Source code in pika/adapters/blocking_connection.py
def cancel(self) -> int:
    """
    Cancel the queue consumer created by `BlockingChannel.consume`, rejecting all pending
    ackable messages.

    NOTE: If you're looking to cancel a consumer issued with
    BlockingChannel.basic_consume then you should call
    BlockingChannel.basic_cancel.

    :returns: The number of messages requeued by Basic.Nack.
        NEW in 0.10.0: returns 0
    """
    if self._queue_consumer_generator is None:
        LOGGER.warning('cancel: queue consumer generator is inactive '
                       '(already cancelled by client or broker?)')
        return 0

    try:
        _, auto_ack, _ = self._queue_consumer_generator.params
        if not auto_ack:
            # Reject messages held by queue consumer generator; NOTE: we
            # can't use basic_nack with the multiple option to avoid nacking
            # messages already held by our client.
            pending_events = self._queue_consumer_generator.pending_events
            # NOTE `get_waiting_message_count` adjusts for `Basic.Cancel`
            #      from the server at the end (if any)
            for _ in range(self.get_waiting_message_count()):
                evt = pending_events.popleft()
                self._impl.basic_reject(evt.method.delivery_tag,
                                        requeue=True)

        self.basic_cancel(self._queue_consumer_generator.consumer_tag)
    finally:
        self._queue_consumer_generator = None

    # Return 0 for compatibility with legacy implementation; the number of
    # nacked messages is not meaningful since only messages consumed with
    # auto_ack=False may be nacked, and those arriving after calling
    # basic_cancel will be rejected automatically by impl channel, so we'll
    # never know how many of those were nacked.
    return 0

close

close(
    reply_code: int = 0, reply_text: str = 'Normal shutdown'
) -> None

Will invoke a clean shutdown of the channel with the AMQP Broker.

PARAMETER DESCRIPTION
reply_code

The reply code to close the channel with

TYPE: int DEFAULT: 0

reply_text

The reply text to close the channel with

TYPE: str DEFAULT: 'Normal shutdown'

Source code in pika/adapters/blocking_connection.py
def close(self,
          reply_code: int = 0,
          reply_text: str = 'Normal shutdown') -> None:
    """
    Will invoke a clean shutdown of the channel with the AMQP Broker.

    :param reply_code: The reply code to close the channel with
    :param reply_text: The reply text to close the channel with
    """
    LOGGER.debug('Channel.close(%s, %s)', reply_code, reply_text)

    self._impl._raise_if_not_open()

    try:
        # Cancel remaining consumers
        self._cancel_all_consumers()

        # Close the channel
        self._impl.close(reply_code=reply_code, reply_text=reply_text)
        self._flush_output(lambda: self.is_closed)
    finally:
        self._cleanup()

confirm_delivery

confirm_delivery() -> None

Turn on RabbitMQ-proprietary Confirm mode in the channel.

For more information see: https://www.rabbitmq.com/confirms.html

Source code in pika/adapters/blocking_connection.py
def confirm_delivery(self) -> None:
    """
    Turn on RabbitMQ-proprietary Confirm mode in the channel.

    For more information see:
        https://www.rabbitmq.com/confirms.html
    """
    if self._delivery_confirmation:
        LOGGER.error(
            'confirm_delivery: confirmation was already enabled '
            'on channel=%s', self.channel_number)
        return

    with _CallbackResult() as select_ok_result:
        self._impl.confirm_delivery(
            ack_nack_callback=self._message_confirmation_result.
            set_value_once,
            callback=select_ok_result.signal_once)

        self._flush_output(select_ok_result.is_ready)

    self._delivery_confirmation = True

    # Unroutable messages returned after this point will be in the context
    # of publisher acknowledgments
    self._impl.add_on_return_callback(self._on_puback_message_returned)

consume

consume(
    queue: str,
    auto_ack: bool = False,
    exclusive: bool = False,
    arguments: dict[str, Any] | None = None,
    inactivity_timeout: float | None = None,
    consumer_tag: str | None = None,
) -> Generator[
    tuple[
        Deliver | None, BasicProperties | None, bytes | None
    ]
]

Blocking consumption of a queue instead of via a callback.

This method is a generator that yields each message as a tuple of method, properties, and body. The active generator iterator terminates when the consumer is cancelled by client via BlockingChannel.cancel() or by broker.

Example: :: for method, properties, body in channel.consume('queue'): print(body) channel.basic_ack(method.delivery_tag)

You should call BlockingChannel.cancel() when you escape out of the generator loop.

If you don't cancel this consumer, then next call on the same channel to consume() with the exact same (queue, auto_ack, exclusive) parameters will resume the existing consumer generator; however, calling with different parameters will result in an exception.

:yields: tuple(spec.Basic.Deliver, spec.BasicProperties, bytes)

PARAMETER DESCRIPTION
queue

The queue name to consume

TYPE: str

auto_ack

Tell the broker to not expect a ack/nack response

TYPE: bool DEFAULT: False

exclusive

Don't allow other consumers on the queue

TYPE: bool DEFAULT: False

arguments

Custom key/value pair arguments for the consumer

TYPE: dict[str, Any] | None DEFAULT: None

inactivity_timeout

if a number is given (in seconds), will cause the method to yield (None, None, None) after the given period of inactivity; this permits for pseudo-regular maintenance activities to be carried out by the user while waiting for messages to arrive. If None is given (default), then the method blocks until the next event arrives. NOTE that timing granularity is limited by the timer resolution of the underlying implementation. NEW in pika 0.10.0.

TYPE: float | None DEFAULT: None

consumer_tag

Specify your own consumer tag

TYPE: str | None DEFAULT: None

RAISES DESCRIPTION
ValueError

if consumer-creation parameters don't match those of the existing queue consumer generator, if any. NEW in pika 0.10.0

ChannelClosed

when this channel is closed by broker.

Source code in pika/adapters/blocking_connection.py
def consume(
    self,
    queue: str,
    auto_ack: bool = False,
    exclusive: bool = False,
    arguments: dict[str, Any] | None = None,
    inactivity_timeout: float | None = None,
    consumer_tag: str | None = None
) -> Generator[tuple[pika.spec.Basic.Deliver | None,
                     pika.spec.BasicProperties | None, bytes | None]]:
    """
    Blocking consumption of a queue instead of via a callback.

    This method is a generator that yields each message as a tuple of method,
    properties, and body. The active generator iterator terminates when the
    consumer is cancelled by client via `BlockingChannel.cancel()` or by
    broker.

    Example:
    ::
        for method, properties, body in channel.consume('queue'):
            print(body)
            channel.basic_ack(method.delivery_tag)

    You should call `BlockingChannel.cancel()` when you escape out of the
    generator loop.

    If you don't cancel this consumer, then next call on the same channel
    to `consume()` with the exact same (queue, auto_ack, exclusive) parameters
    will resume the existing consumer generator; however, calling with
    different parameters will result in an exception.

    :param queue: The queue name to consume
    :param auto_ack: Tell the broker to not expect a ack/nack response
    :param exclusive: Don't allow other consumers on the queue
    :param arguments: Custom key/value pair arguments for the consumer
    :param inactivity_timeout: if a number is given (in
        seconds), will cause the method to yield (None, None, None) after the
        given period of inactivity; this permits for pseudo-regular maintenance
        activities to be carried out by the user while waiting for messages
        to arrive. If None is given (default), then the method blocks until
        the next event arrives. NOTE that timing granularity is limited by
        the timer resolution of the underlying implementation.
        NEW in pika 0.10.0.
    :param consumer_tag: Specify your own consumer tag

    :yields: tuple(spec.Basic.Deliver, spec.BasicProperties, bytes)

    :raises ValueError: if consumer-creation parameters don't match those
        of the existing queue consumer generator, if any.
        NEW in pika 0.10.0
    :raises ChannelClosed: when this channel is closed by broker.
    """
    self._impl._raise_if_not_open()

    params = (queue, auto_ack, exclusive)

    if self._queue_consumer_generator is not None:
        if params != self._queue_consumer_generator.params:
            raise ValueError(
                'Consume with different params not allowed on existing '
                f'queue consumer generator; previous params: {self._queue_consumer_generator.params!r}; '
                f'new params: {(queue, auto_ack, exclusive)!r}')
    else:
        LOGGER.debug('Creating new queue consumer generator; params: %r',
                     params)
        # Need a consumer tag to register consumer info before sending
        # request to broker, because I/O might pick up incoming messages
        # in addition to Basic.Consume-ok
        if not consumer_tag:
            consumer_tag = self._impl._generate_consumer_tag()

        self._queue_consumer_generator = _QueueConsumerGeneratorInfo(
            params, consumer_tag)

        try:
            self._basic_consume_impl(
                queue=queue,
                auto_ack=auto_ack,
                exclusive=exclusive,
                consumer_tag=consumer_tag,
                arguments=arguments,
                alternate_event_sink=self._on_consumer_generator_event)
        except Exception:
            self._queue_consumer_generator = None
            raise

        LOGGER.info('Created new queue consumer generator %r',
                    self._queue_consumer_generator)

    while self._queue_consumer_generator is not None:
        # Process pending events
        if self._queue_consumer_generator.pending_events:
            evt = self._queue_consumer_generator.pending_events.popleft()
            if type(evt) is _ConsumerCancellationEvt:
                # Consumer was cancelled by broker
                self._queue_consumer_generator = None
                break
            else:
                yield (evt.method, evt.properties, evt.body)
                continue

        if inactivity_timeout is None:
            # Wait indefinitely for a message to arrive, while processing
            # I/O events and triggering ChannelClosed exception when the
            # channel fails
            self._process_data_events(time_limit=None)
            continue

        # Wait with inactivity timeout
        wait_start_time = pika._utils.time_now()
        wait_deadline = wait_start_time + inactivity_timeout
        delta = inactivity_timeout

        while (self._queue_consumer_generator is not None and
               not self._queue_consumer_generator.pending_events):

            self._process_data_events(time_limit=delta)

            if not self._queue_consumer_generator:
                # Consumer was cancelled by client
                break

            if self._queue_consumer_generator.pending_events:
                # Got message(s)
                break

            delta = wait_deadline - pika._utils.time_now()
            if delta <= 0.0:
                # Signal inactivity timeout
                yield (None, None, None)
                break

exchange_bind

exchange_bind(
    destination: str,
    source: str,
    routing_key: str = '',
    arguments: dict[str, Any] | None = None,
) -> Method[BindOk]

Bind an exchange to another exchange.

PARAMETER DESCRIPTION
destination

The destination exchange to bind

TYPE: str

source

The source exchange to bind to

TYPE: str

routing_key

The routing key to bind on

TYPE: str DEFAULT: ''

arguments

Custom key/value pair arguments for the binding

TYPE: dict[str, Any] | None DEFAULT: None

RETURNS DESCRIPTION
Method[BindOk]

Method frame from the Exchange.Bind-ok response

Source code in pika/adapters/blocking_connection.py
def exchange_bind(
    self,
    destination: str,
    source: str,
    routing_key: str = '',
    arguments: dict[str, Any] | None = None
) -> pika.frame.Method[pika.spec.Exchange.BindOk]:
    """
    Bind an exchange to another exchange.

    :param destination: The destination exchange to bind
    :param source: The source exchange to bind to
    :param routing_key: The routing key to bind on
    :param arguments: Custom key/value pair arguments for the binding
    :returns: Method frame from the Exchange.Bind-ok response
    """
    validators.require_string(destination, 'destination')
    validators.require_string(source, 'source')
    with _CallbackResult(
            self._MethodFrameCallbackResultArgs) as bind_ok_result:
        self._impl.exchange_bind(destination=destination,
                                 source=source,
                                 routing_key=routing_key,
                                 arguments=arguments,
                                 callback=bind_ok_result.set_value_once)

        self._flush_output(bind_ok_result.is_ready)
        return bind_ok_result.value.method_frame

exchange_declare

exchange_declare(
    exchange: str,
    exchange_type: ExchangeType | str = direct,
    passive: bool = False,
    durable: bool = False,
    auto_delete: bool = False,
    internal: bool = False,
    arguments: dict[str, Any] | None = None,
) -> None

This method creates an exchange if it does not already exist, and if the exchange exists, verifies that it is of the correct and expected class.

If passive set, the server will reply with Declare-Ok if the exchange already exists with the same name, and raise an error if not and if the exchange does not already exist, the server MUST raise a channel exception with reply code 404 (not found).

PARAMETER DESCRIPTION
exchange

The exchange name consists of a non-empty sequence of these characters: letters, digits, hyphen, underscore, period, or colon.

TYPE: str

exchange_type

The exchange type to use

TYPE: ExchangeType | str DEFAULT: direct

passive

Perform a declare or just check to see if it exists

TYPE: bool DEFAULT: False

durable

Survive a reboot of RabbitMQ

TYPE: bool DEFAULT: False

auto_delete

Remove when no more queues are bound to it

TYPE: bool DEFAULT: False

internal

Can only be published to by other exchanges

TYPE: bool DEFAULT: False

arguments

Custom key/value pair arguments for the exchange

TYPE: dict[str, Any] | None DEFAULT: None

RETURNS DESCRIPTION
None

Method frame from the Exchange.Declare-ok response

Source code in pika/adapters/blocking_connection.py
def exchange_declare(self,
                     exchange: str,
                     exchange_type: (ExchangeType |
                                     str) = ExchangeType.direct,
                     passive: bool = False,
                     durable: bool = False,
                     auto_delete: bool = False,
                     internal: bool = False,
                     arguments: dict[str, Any] | None = None) -> None:
    """
    This method creates an exchange if it does not already exist, and if the exchange exists,
    verifies that it is of the correct and expected class.

    If passive set, the server will reply with Declare-Ok if the exchange
    already exists with the same name, and raise an error if not and if the
    exchange does not already exist, the server MUST raise a channel
    exception with reply code 404 (not found).

    :param exchange: The exchange name consists of a non-empty sequence of
                      these characters: letters, digits, hyphen, underscore,
                      period, or colon.
    :param exchange_type: The exchange type to use
    :param passive: Perform a declare or just check to see if it exists
    :param durable: Survive a reboot of RabbitMQ
    :param auto_delete: Remove when no more queues are bound to it
    :param internal: Can only be published to by other exchanges
    :param arguments: Custom key/value pair arguments for the exchange
    :returns: Method frame from the Exchange.Declare-ok response
    """
    validators.require_string(exchange, 'exchange')
    with _CallbackResult(
            self._MethodFrameCallbackResultArgs) as declare_ok_result:
        self._impl.exchange_declare(
            exchange=exchange,
            exchange_type=exchange_type,
            passive=passive,
            durable=durable,
            auto_delete=auto_delete,
            internal=internal,
            arguments=arguments,
            callback=declare_ok_result.set_value_once)

        self._flush_output(declare_ok_result.is_ready)
        return declare_ok_result.value.method_frame

exchange_delete

exchange_delete(
    exchange: str | None = None, if_unused: bool = False
) -> Method[DeleteOk]

Delete the exchange.

PARAMETER DESCRIPTION
exchange

The exchange name

TYPE: str | None DEFAULT: None

if_unused

only delete if the exchange is unused

TYPE: bool DEFAULT: False

RETURNS DESCRIPTION
Method[DeleteOk]

Method frame from the Exchange.Delete-ok response

Source code in pika/adapters/blocking_connection.py
def exchange_delete(self,
                    exchange: str | None = None,
                    if_unused: bool = False
                   ) -> pika.frame.Method[pika.spec.Exchange.DeleteOk]:
    """
    Delete the exchange.

    :param exchange: The exchange name
    :param if_unused: only delete if the exchange is unused
    :returns: Method frame from the Exchange.Delete-ok response
    """
    with _CallbackResult(
            self._MethodFrameCallbackResultArgs) as delete_ok_result:
        self._impl.exchange_delete(exchange=exchange,
                                   if_unused=if_unused,
                                   callback=delete_ok_result.set_value_once)

        self._flush_output(delete_ok_result.is_ready)
        return delete_ok_result.value.method_frame

exchange_unbind

exchange_unbind(
    destination: str | None = None,
    source: str | None = None,
    routing_key: str = '',
    arguments: dict[str, Any] | None = None,
) -> Method[UnbindOk]

Unbind an exchange from another exchange.

PARAMETER DESCRIPTION
destination

The destination exchange to unbind

TYPE: str | None DEFAULT: None

source

The source exchange to unbind from

TYPE: str | None DEFAULT: None

routing_key

The routing key to unbind

TYPE: str DEFAULT: ''

arguments

Custom key/value pair arguments for the binding

TYPE: dict[str, Any] | None DEFAULT: None

RETURNS DESCRIPTION
Method[UnbindOk]

Method frame from the Exchange.Unbind-ok response

Source code in pika/adapters/blocking_connection.py
def exchange_unbind(
    self,
    destination: str | None = None,
    source: str | None = None,
    routing_key: str = '',
    arguments: dict[str, Any] | None = None
) -> pika.frame.Method[pika.spec.Exchange.UnbindOk]:
    """
    Unbind an exchange from another exchange.

    :param destination: The destination exchange to unbind
    :param source: The source exchange to unbind from
    :param routing_key: The routing key to unbind
    :param arguments: Custom key/value pair arguments for the binding
    :returns: Method frame from the Exchange.Unbind-ok response
    """
    with _CallbackResult(
            self._MethodFrameCallbackResultArgs) as unbind_ok_result:
        self._impl.exchange_unbind(destination=destination,
                                   source=source,
                                   routing_key=routing_key,
                                   arguments=arguments,
                                   callback=unbind_ok_result.set_value_once)

        self._flush_output(unbind_ok_result.is_ready)
        return unbind_ok_result.value.method_frame

flow

flow(active: bool) -> bool

Turn Channel flow control off and on.

NOTE: RabbitMQ doesn't support active=False; per https://www.rabbitmq.com/specification.html: "active=false is not supported by the server. Limiting prefetch with basic.qos provides much better control"

For more information, please reference:

https://www.rabbitmq.com/amqp-0-9-1-reference.html#channel.flow

PARAMETER DESCRIPTION
active

Turn flow on (True) or off (False)

TYPE: bool

RETURNS DESCRIPTION
bool

True if broker will start or continue sending; False if not

Source code in pika/adapters/blocking_connection.py
def flow(self, active: bool) -> bool:
    """
    Turn Channel flow control off and on.

    NOTE: RabbitMQ doesn't support active=False; per
    https://www.rabbitmq.com/specification.html: "active=false is not
    supported by the server. Limiting prefetch with basic.qos provides much
    better control"

    For more information, please reference:

    https://www.rabbitmq.com/amqp-0-9-1-reference.html#channel.flow

    :param active: Turn flow on (True) or off (False)
    :returns: True if broker will start or continue sending; False if not
    """
    with _CallbackResult(self._FlowOkCallbackResultArgs) as flow_ok_result:
        self._impl.flow(active=active,
                        callback=flow_ok_result.set_value_once)
        self._flush_output(flow_ok_result.is_ready)
        return flow_ok_result.value.active

get_waiting_message_count

get_waiting_message_count() -> int

Returns the number of messages that may be retrieved from the current queue consumer generator via BlockingChannel.consume without blocking. NEW in pika 0.10.0.

RETURNS DESCRIPTION
int

The number of waiting messages

Source code in pika/adapters/blocking_connection.py
def get_waiting_message_count(self) -> int:
    """
    Returns the number of messages that may be retrieved from the current queue consumer
    generator via `BlockingChannel.consume` without blocking. NEW in pika 0.10.0.

    :returns: The number of waiting messages
    """
    if self._queue_consumer_generator is not None:
        pending_events = self._queue_consumer_generator.pending_events
        count = len(pending_events)
        if count and type(pending_events[-1]) is _ConsumerCancellationEvt:
            count -= 1
    else:
        count = 0

    return count

queue_bind

queue_bind(
    queue: str,
    exchange: str,
    routing_key: str | None = None,
    arguments: dict[str, Any] | None = None,
) -> Method[BindOk]

Bind the queue to the specified exchange.

PARAMETER DESCRIPTION
queue

The queue to bind to the exchange

TYPE: str

exchange

The source exchange to bind to

TYPE: str

routing_key

The routing key to bind on

TYPE: str | None DEFAULT: None

arguments

Custom key/value pair arguments for the binding

TYPE: dict[str, Any] | None DEFAULT: None

RETURNS DESCRIPTION
Method[BindOk]

Method frame from the Queue.Bind-ok response

Source code in pika/adapters/blocking_connection.py
def queue_bind(
    self,
    queue: str,
    exchange: str,
    routing_key: str | None = None,
    arguments: dict[str, Any] | None = None
) -> pika.frame.Method[pika.spec.Queue.BindOk]:
    """
    Bind the queue to the specified exchange.

    :param queue: The queue to bind to the exchange
    :param exchange: The source exchange to bind to
    :param routing_key: The routing key to bind on
    :param arguments: Custom key/value pair arguments for the binding
    :returns: Method frame from the Queue.Bind-ok response
    """
    validators.require_string(queue, 'queue')
    validators.require_string(exchange, 'exchange')
    with _CallbackResult(
            self._MethodFrameCallbackResultArgs) as bind_ok_result:
        self._impl.queue_bind(queue=queue,
                              exchange=exchange,
                              routing_key=routing_key,
                              arguments=arguments,
                              callback=bind_ok_result.set_value_once)
        self._flush_output(bind_ok_result.is_ready)
        return bind_ok_result.value.method_frame

queue_declare

queue_declare(
    queue: str,
    passive: bool = False,
    durable: bool = False,
    exclusive: bool = False,
    auto_delete: bool = False,
    arguments: dict[str, Any] | None = None,
) -> Method[DeclareOk]

Declare queue, create if needed.

This method creates or checks a queue. When creating a new queue the client can specify various properties that control the durability of the queue and its contents, and the level of sharing for the queue.

Use an empty string as the queue name for the broker to auto-generate one. Retrieve this auto-generated queue name from the returned spec.Queue.DeclareOk method frame.

PARAMETER DESCRIPTION
queue

The queue name; if empty string, the broker will create a unique queue name

TYPE: str

passive

Only check to see if the queue exists and raise ChannelClosed if it doesn't

TYPE: bool DEFAULT: False

durable

Survive reboots of the broker

TYPE: bool DEFAULT: False

exclusive

Only allow access by the current connection

TYPE: bool DEFAULT: False

auto_delete

Delete after consumer cancels or disconnects

TYPE: bool DEFAULT: False

arguments

Custom key/value arguments for the queue

TYPE: dict[str, Any] | None DEFAULT: None

RETURNS DESCRIPTION
Method[DeclareOk]

Method frame from the Queue.Declare-ok response

Source code in pika/adapters/blocking_connection.py
def queue_declare(
    self,
    queue: str,
    passive: bool = False,
    durable: bool = False,
    exclusive: bool = False,
    auto_delete: bool = False,
    arguments: dict[str, Any] | None = None
) -> pika.frame.Method[pika.spec.Queue.DeclareOk]:
    """
    Declare queue, create if needed.

    This method creates or checks a queue. When creating a new queue the client can specify various
    properties that control the durability of the queue and its contents,
    and the level of sharing for the queue.

    Use an empty string as the queue name for the broker to auto-generate
    one. Retrieve this auto-generated queue name from the returned
    `spec.Queue.DeclareOk` method frame.

    :param queue: The queue name; if empty string, the broker will
        create a unique queue name
    :param passive: Only check to see if the queue exists and raise
      `ChannelClosed` if it doesn't
    :param durable: Survive reboots of the broker
    :param exclusive: Only allow access by the current connection
    :param auto_delete: Delete after consumer cancels or disconnects
    :param arguments: Custom key/value arguments for the queue
    :returns: Method frame from the Queue.Declare-ok response
    """
    validators.require_string(queue, 'queue')
    with _CallbackResult(
            self._MethodFrameCallbackResultArgs) as declare_ok_result:
        self._impl.queue_declare(queue=queue,
                                 passive=passive,
                                 durable=durable,
                                 exclusive=exclusive,
                                 auto_delete=auto_delete,
                                 arguments=arguments,
                                 callback=declare_ok_result.set_value_once)

        self._flush_output(declare_ok_result.is_ready)
        return declare_ok_result.value.method_frame

queue_delete

queue_delete(
    queue: str,
    if_unused: bool = False,
    if_empty: bool = False,
) -> Method[DeleteOk]

Delete a queue from the broker.

PARAMETER DESCRIPTION
queue

The queue to delete

TYPE: str

if_unused

only delete if it's unused

TYPE: bool DEFAULT: False

if_empty

only delete if the queue is empty

TYPE: bool DEFAULT: False

RETURNS DESCRIPTION
Method[DeleteOk]

Method frame from the Queue.Delete-ok response

Source code in pika/adapters/blocking_connection.py
def queue_delete(self,
                 queue: str,
                 if_unused: bool = False,
                 if_empty: bool = False
                ) -> pika.frame.Method[pika.spec.Queue.DeleteOk]:
    """
    Delete a queue from the broker.

    :param queue: The queue to delete
    :param if_unused: only delete if it's unused
    :param if_empty: only delete if the queue is empty
    :returns: Method frame from the Queue.Delete-ok response
    """
    with _CallbackResult(
            self._MethodFrameCallbackResultArgs) as delete_ok_result:
        self._impl.queue_delete(queue=queue,
                                if_unused=if_unused,
                                if_empty=if_empty,
                                callback=delete_ok_result.set_value_once)

        self._flush_output(delete_ok_result.is_ready)
        return delete_ok_result.value.method_frame

queue_purge

queue_purge(queue: str) -> Method[PurgeOk]

Purge all of the messages from the specified queue.

PARAMETER DESCRIPTION
queue

The queue to purge

TYPE: str

RETURNS DESCRIPTION
Method[PurgeOk]

Method frame from the Queue.Purge-ok response

Source code in pika/adapters/blocking_connection.py
def queue_purge(self,
                queue: str) -> pika.frame.Method[pika.spec.Queue.PurgeOk]:
    """
    Purge all of the messages from the specified queue.

    :param queue: The queue to purge
    :returns: Method frame from the Queue.Purge-ok response
    """
    with _CallbackResult(
            self._MethodFrameCallbackResultArgs) as purge_ok_result:
        self._impl.queue_purge(queue=queue,
                               callback=purge_ok_result.set_value_once)
        self._flush_output(purge_ok_result.is_ready)
        return purge_ok_result.value.method_frame

queue_unbind

queue_unbind(
    queue: str,
    exchange: str,
    routing_key: str | None = None,
    arguments: dict[str, Any] | None = None,
) -> Method[UnbindOk]

Unbind a queue from an exchange.

PARAMETER DESCRIPTION
queue

The queue to unbind from the exchange

TYPE: str

exchange

The source exchange to bind from

TYPE: str

routing_key

The routing key to unbind

TYPE: str | None DEFAULT: None

arguments

Custom key/value pair arguments for the binding

TYPE: dict[str, Any] | None DEFAULT: None

RETURNS DESCRIPTION
Method[UnbindOk]

Method frame from the Queue.Unbind-ok response

Source code in pika/adapters/blocking_connection.py
def queue_unbind(
    self,
    queue: str,
    exchange: str,
    routing_key: str | None = None,
    arguments: dict[str, Any] | None = None
) -> pika.frame.Method[pika.spec.Queue.UnbindOk]:
    """
    Unbind a queue from an exchange.

    :param queue: The queue to unbind from the exchange
    :param exchange: The source exchange to bind from
    :param routing_key: The routing key to unbind
    :param arguments: Custom key/value pair arguments for the binding
    :returns: Method frame from the Queue.Unbind-ok response
    """
    with _CallbackResult(
            self._MethodFrameCallbackResultArgs) as unbind_ok_result:
        self._impl.queue_unbind(queue=queue,
                                exchange=exchange,
                                routing_key=routing_key,
                                arguments=arguments,
                                callback=unbind_ok_result.set_value_once)
        self._flush_output(unbind_ok_result.is_ready)
        return unbind_ok_result.value.method_frame

start_consuming

start_consuming() -> None

Processes I/O events and dispatches timers and basic_consume callbacks until all consumers are cancelled.

NOTE: this blocking function may not be called from the scope of a pika callback, because dispatching basic_consume callbacks from this context would constitute recursion.

RAISES DESCRIPTION
pika.exceptions.ReentrancyError

if called from the scope of a BlockingConnection or BlockingChannel callback

ChannelClosed

when this channel is closed by broker.

Source code in pika/adapters/blocking_connection.py
def start_consuming(self) -> None:
    """
    Processes I/O events and dispatches timers and `basic_consume` callbacks until all consumers
    are cancelled.

    NOTE: this blocking function may not be called from the scope of a
    pika callback, because dispatching `basic_consume` callbacks from this
    context would constitute recursion.

    :raises pika.exceptions.ReentrancyError: if called from the scope of a
        `BlockingConnection` or `BlockingChannel` callback
    :raises ChannelClosed: when this channel is closed by broker.
    """
    # Check if called from the scope of an event dispatch callback
    with self.connection._acquire_event_dispatch() as dispatch_allowed:
        if not dispatch_allowed:
            raise exceptions.ReentrancyError(
                'start_consuming may not be called from the scope of '
                'another BlockingConnection or BlockingChannel callback')

    self._impl._raise_if_not_open()

    # Process events as long as consumers exist on this channel
    while self._consumer_infos:
        # This will raise ChannelClosed if channel is closed by broker
        self._process_data_events(time_limit=None)

stop_consuming

stop_consuming(consumer_tag: str | None = None) -> None

Cancels all consumers, signalling the start_consuming loop to exit.

NOTE: pending non-ackable messages will be lost; pending ackable messages will be rejected.

PARAMETER DESCRIPTION
consumer_tag

if specified, only the consumer with the given tag will be cancelled

TYPE: str | None DEFAULT: None

Source code in pika/adapters/blocking_connection.py
def stop_consuming(self, consumer_tag: str | None = None) -> None:
    """
    Cancels all consumers, signalling the `start_consuming` loop to exit.

    NOTE: pending non-ackable messages will be lost; pending ackable
    messages will be rejected.

    :param consumer_tag: if specified, only the consumer with the given tag will be cancelled
    """
    if consumer_tag:
        self.basic_cancel(consumer_tag)
    else:
        self._cancel_all_consumers()

tx_commit

tx_commit() -> Method[CommitOk]

Commit a transaction.

RETURNS DESCRIPTION
Method[CommitOk]

Method frame from the Tx.Commit-ok response

Source code in pika/adapters/blocking_connection.py
def tx_commit(self) -> pika.frame.Method[pika.spec.Tx.CommitOk]:
    """
    Commit a transaction.

    :returns: Method frame from the Tx.Commit-ok response
    """
    with _CallbackResult(
            self._MethodFrameCallbackResultArgs) as commit_ok_result:
        self._impl.tx_commit(commit_ok_result.set_value_once)

        self._flush_output(commit_ok_result.is_ready)
        return commit_ok_result.value.method_frame

tx_rollback

tx_rollback() -> Method[RollbackOk]

Rollback a transaction.

RETURNS DESCRIPTION
Method[RollbackOk]

Method frame from the Tx.Rollback-ok response

Source code in pika/adapters/blocking_connection.py
def tx_rollback(self) -> pika.frame.Method[pika.spec.Tx.RollbackOk]:
    """
    Rollback a transaction.

    :returns: Method frame from the Tx.Rollback-ok response
    """
    with _CallbackResult(
            self._MethodFrameCallbackResultArgs) as rollback_ok_result:
        self._impl.tx_rollback(rollback_ok_result.set_value_once)

        self._flush_output(rollback_ok_result.is_ready)
        return rollback_ok_result.value.method_frame

tx_select

tx_select() -> Method[SelectOk]

Select standard transaction mode.

This method sets the channel to use standard transactions. The client must use this method at least once on a channel before using the Commit or Rollback methods.

RETURNS DESCRIPTION
Method[SelectOk]

Method frame from the Tx.Select-ok response

Source code in pika/adapters/blocking_connection.py
def tx_select(self) -> pika.frame.Method[pika.spec.Tx.SelectOk]:
    """
    Select standard transaction mode.

    This method sets the channel to use standard transactions. The client must use this method
    at least once on a channel before using the Commit or Rollback methods.

    :returns: Method frame from the Tx.Select-ok response
    """
    with _CallbackResult(
            self._MethodFrameCallbackResultArgs) as select_ok_result:
        self._impl.tx_select(select_ok_result.set_value_once)

        self._flush_output(select_ok_result.is_ready)
        return select_ok_result.value.method_frame

BlockingConnection

The BlockingConnection creates a layer on top of Pika's asynchronous core providing methods that will block until their expected response has returned. Due to the asynchronous nature of the Basic.Deliver and Basic.Return calls from RabbitMQ to your application, you can still implement continuation-passing style asynchronous methods if you'd like to receive messages from RabbitMQ using :meth:basic_consume <BlockingChannel.basic_consume> or if you want to be notified of a delivery failure when using :meth:basic_publish <BlockingChannel.basic_publish>.

For more information about communicating with the blocking_connection adapter, be sure to check out the :class:BlockingChannel <BlockingChannel> class which implements the :class:Channel <pika.channel.Channel> based communication for the blocking_connection adapter.

To prevent recursion/reentrancy, the blocking connection and channel implementations queue asynchronously-delivered events received in nested context (e.g., while waiting for BlockingConnection.channel or BlockingChannel.queue_declare to complete), dispatching them synchronously once nesting returns to the desired context. This concerns all callbacks, such as those registered via BlockingConnection.call_later, BlockingConnection.add_on_connection_blocked_callback, BlockingConnection.add_on_connection_unblocked_callback, BlockingChannel.basic_consume, etc.

Blocked Connection deadlock avoidance: when RabbitMQ becomes low on resources, it emits Connection.Blocked (AMQP extension) to the client connection when client makes a resource-consuming request on that connection or its channel (e.g., Basic.Publish); subsequently, RabbitMQ suspsends processing requests from that connection until the affected resources are restored. See https://www.rabbitmq.com/connection-blocked.html. This may impact BlockingConnection and BlockingChannel operations in a way that users might not be expecting. For example, if the user dispatches BlockingChannel.basic_publish in non-publisher-confirmation mode while RabbitMQ is in this low-resource state followed by a synchronous request (e.g., BlockingConnection.channel, BlockingChannel.consume, BlockingChannel.basic_consume, etc.), the synchronous request will block indefinitely (until Connection.Unblocked) waiting for RabbitMQ to reply. If the blocked state persists for a long time, the blocking operation will appear to hang. In this state, BlockingConnection instance and its channels will not dispatch user callbacks. SOLUTION: To break this potential deadlock, applications may configure the blocked_connection_timeout connection parameter when instantiating BlockingConnection. Upon blocked connection timeout, this adapter will raise ConnectionBlockedTimeout exception. Seepika.connection.ConnectionParametersdocumentation to learn more about theblocked_connection_timeout` configuration.

Source code in pika/adapters/blocking_connection.py
class BlockingConnection:
    """The BlockingConnection creates a layer on top of Pika's asynchronous core
    providing methods that will block until their expected response has
    returned. Due to the asynchronous nature of the `Basic.Deliver` and
    `Basic.Return` calls from RabbitMQ to your application, you can still
    implement continuation-passing style asynchronous methods if you'd like to
    receive messages from RabbitMQ using
    :meth:`basic_consume <BlockingChannel.basic_consume>` or if you want to be
    notified of a delivery failure when using
    :meth:`basic_publish <BlockingChannel.basic_publish>`.

    For more information about communicating with the blocking_connection
    adapter, be sure to check out the
    :class:`BlockingChannel <BlockingChannel>` class which implements the
    :class:`Channel <pika.channel.Channel>` based communication for the
    blocking_connection adapter.

    To prevent recursion/reentrancy, the blocking connection and channel
    implementations queue asynchronously-delivered events received
    in nested context (e.g., while waiting for `BlockingConnection.channel` or
    `BlockingChannel.queue_declare` to complete), dispatching them synchronously
    once nesting returns to the desired context. This concerns all callbacks,
    such as those registered via `BlockingConnection.call_later`,
    `BlockingConnection.add_on_connection_blocked_callback`,
    `BlockingConnection.add_on_connection_unblocked_callback`,
    `BlockingChannel.basic_consume`, etc.

    Blocked Connection deadlock avoidance: when RabbitMQ becomes low on
    resources, it emits Connection.Blocked (AMQP extension) to the client
    connection when client makes a resource-consuming request on that connection
    or its channel (e.g., `Basic.Publish`); subsequently, RabbitMQ suspsends
    processing requests from that connection until the affected resources are
    restored. See https://www.rabbitmq.com/connection-blocked.html. This
    may impact `BlockingConnection` and `BlockingChannel` operations in a
    way that users might not be expecting. For example, if the user dispatches
    `BlockingChannel.basic_publish` in non-publisher-confirmation mode while
    RabbitMQ is in this low-resource state followed by a synchronous request
    (e.g., `BlockingConnection.channel`, `BlockingChannel.consume`,
    `BlockingChannel.basic_consume`, etc.), the synchronous request will block
    indefinitely (until Connection.Unblocked) waiting for RabbitMQ to reply. If
    the blocked state persists for a long time, the blocking operation will
    appear to hang. In this state, `BlockingConnection` instance and its
    channels will not dispatch user callbacks. SOLUTION: To break this potential
    deadlock, applications may configure the `blocked_connection_timeout`
    connection parameter when instantiating `BlockingConnection`. Upon blocked
    connection timeout, this adapter will raise ConnectionBlockedTimeout
    exception`. See `pika.connection.ConnectionParameters` documentation to
    learn more about the `blocked_connection_timeout` configuration.

    """

    class _OnClosedArgs(NamedTuple):
        connection: Any
        error: Any

    class _OnChannelOpenedArgs(NamedTuple):
        channel: Any

    def __init__(
            self,
            parameters: pika.connection.Parameters |
        Sequence[pika.connection.Parameters] | None = None,
            _impl_class: select_connection.SelectConnection | None = None
    ) -> None:
        """
        Create a new instance of the Connection object.

        :param parameters:
            Connection parameters instance or non-empty sequence of them. If
            None, a `pika.connection.Parameters` instance will be created with
            default settings. See `pika.AMQPConnectionWorkflow` for more
            details about multiple parameter configurations and retries.
            Type: `None | pika.connection.Parameters | sequence`.
        :param _impl_class: for tests/debugging only; implementation class;
            None=default

        :raises RuntimeError:
        """
        warnings.warn(
            'BlockingConnection is deprecated and will be removed in Pika 2.0. '
            'Use Connection instead, which runs its own IOLoop on a '
            'background thread and provides a thread-safe blocking API that '
            'does not stall heartbeats on slow message processing. See '
            'https://pika.github.io/pika/latest/modules/adapters/thread_safe/',
            DeprecationWarning,
            stacklevel=2,
        )
        # Used for mutual exclusion to avoid race condition between
        # BlockingConnection._cleanup() and another thread calling
        # BlockingConnection.add_callback_threadsafe() against a closed
        # ioloop.
        self._cleanup_mutex = threading.Lock()

        # Used by the _acquire_event_dispatch decorator; when already greater
        # than 0, event dispatch is already acquired higher up the call stack
        self._event_dispatch_suspend_depth = 0

        # Connection-specific events that are ready for dispatch: _TimerEvt,
        # _ConnectionBlockedEvt, _ConnectionUnblockedEvt
        self._ready_events: deque[Any] = deque()

        # Channel numbers of channels that are requesting a call to their
        # BlockingChannel._dispatch_events method; See
        # `_request_channel_dispatch`
        self._channels_pending_dispatch: set[int] = set()

        # Receives on_close_callback args from Connection
        self._closed_result = _CallbackResult(self._OnClosedArgs)

        # Store exceptions for server-initiated channel closures
        self._server_channel_closures: deque[Exception] = deque()

        # Declared without a value so that the type stays non-optional for the
        # public API, all of which runs only after a successful workflow.
        # `_create_connection()` may raise before this is ever assigned, so the
        # two members that can run on a half-built instance, `_cleanup()` and
        # `__repr__()`, read it with `getattr()`.
        self._impl: select_connection.SelectConnection

        self._impl = self._create_connection(parameters, _impl_class)
        self._impl.add_on_close_callback(self._closed_result.set_value_once)

    @override
    def __repr__(self) -> str:
        # `getattr` because `__repr__` runs on a half-built instance whenever
        # `__init__` raises: a traceback or debugger rendering the frame's locals
        # must not turn the real error into an `AttributeError`.
        impl = getattr(self, '_impl', None)
        return f'<{self.__class__.__name__} impl={impl!r}>'

    def __enter__(self) -> Self:
        # Prepare `with` context
        return self

    def __exit__(self, exc_type: type[BaseException] | None,
                 value: BaseException | None,
                 traceback: TracebackType | None) -> None:
        # Close connection after `with` context
        if self.is_open:
            self.close()

    def _cleanup(self) -> None:
        """Clean up members that might inhibit garbage collection."""
        with self._cleanup_mutex:
            # `getattr` rather than a plain attribute test: `__init__` may have
            # raised before `_impl` was assigned.
            if getattr(self, '_impl', None) is not None:
                self._impl.ioloop.close()
            self._ready_events.clear()
            self._closed_result.reset()

    @contextlib.contextmanager
    def _acquire_event_dispatch(self) -> Generator[bool, Any, None]:
        """
        Context manager that controls access to event dispatcher for preventing reentrancy.

        The "as" value is True if the managed code block owns the event dispatcher and False if
        caller higher up in the call stack already owns it. Only managed code that gets ownership
        (got True) is permitted to dispatch
        """
        try:
            # __enter__ part
            self._event_dispatch_suspend_depth += 1
            yield self._event_dispatch_suspend_depth == 1
        finally:
            # __exit__ part
            self._event_dispatch_suspend_depth -= 1

    def _create_connection(
        self,
        configs: pika.connection.Parameters |
        Sequence[pika.connection.Parameters] | None = None,
        impl_class: select_connection.SelectConnection | None = None
    ) -> select_connection.SelectConnection:
        """
        Run connection workflow, blocking until it completes.

        :param configs: Connection
            parameters instance or non-empty sequence of them.
            Type: `None | pika.connection.Parameters | sequence`.
        :param impl_class: for tests/debugging only; implementation class.


        :raises Exception: on failure
        """
        if configs is None:
            configs = (pika.connection.Parameters(),)

        if isinstance(configs, pika.connection.Parameters):
            configs = (configs,)

        if not configs:
            raise ValueError('Expected a non-empty sequence of connection '
                             f'parameters, but got {configs!r}.')

        # Connection workflow completion args
        #   `result` may be an instance of connection on success or exception on
        #   failure.
        on_cw_done_result = _CallbackResult(
            namedtuple('BlockingConnection_OnConnectionWorkflowDoneArgs',
                       'result'))

        resolved_impl_class = impl_class or select_connection.SelectConnection

        ioloop = select_connection.IOLoop()

        ioloop.activate_poller()
        try:
            resolved_impl_class.create_connection(
                configs,
                on_done=on_cw_done_result.set_value_once,
                custom_ioloop=ioloop)

            while not on_cw_done_result.ready:
                ioloop.poll()
                ioloop.process_timeouts()

            if isinstance(on_cw_done_result.value.result, BaseException):
                error = on_cw_done_result.value.result
                LOGGER.error('Connection workflow failed: %r', error)
                raise self._reap_last_connection_workflow_error(error)
            LOGGER.info('Connection workflow succeeded: %r',
                        on_cw_done_result.value.result)
            return on_cw_done_result.value.result
        except Exception:
            LOGGER.exception('Error in _create_connection().')
            ioloop.close()
            self._cleanup()
            raise

    @staticmethod
    def _reap_last_connection_workflow_error(
            error: BaseException) -> BaseException:
        """
        Extract exception value from the last connection attempt.

        :param error: error passed by the `AMQPConnectionWorkflow` completion callback.
        :returns: Exception value from the last connection attempt
        """
        if isinstance(error, connection_workflow.AMQPConnectionWorkflowFailed):
            # Extract exception value from the last connection attempt
            last = error.exceptions[-1]
            if isinstance(last,
                          connection_workflow.AMQPConnectorSocketConnectError):
                error = exceptions.AMQPConnectionError(last,
                                                       host=last.host,
                                                       port=last.port)
            elif isinstance(last,
                            connection_workflow.AMQPConnectorPhaseErrorBase):
                error = last.exception
            else:
                error = last

        return error

    def _flush_output(self, *waiters: Callable[[], bool]) -> None:
        """
        Flush output and process input while waiting for any of the given callbacks to return true.
        The wait is aborted upon connection-close. Otherwise, processing continues until the output
        is flushed AND at least one of the callbacks returns true. If there are no callbacks, then
        processing ends when all output is flushed.

        :param waiters: sequence of zero or more callables taking no args and returning true when
            it's time to stop processing. Their results are OR'ed together.
        :raises Exception: exceptions passed by impl if opening of connection fails or connection
            closes.
        """
        if self.is_closed:
            raise exceptions.ConnectionWrongStateError()

        # Conditions for terminating the processing loop:
        #   connection closed
        #         OR
        #   empty outbound buffer and no waiters
        #         OR
        #   empty outbound buffer and any waiter is ready
        def is_done() -> bool:
            return (self._closed_result.ready or
                    ((not self._impl._transport or
                      self._impl._get_write_buffer_size() == 0) and
                     (not waiters or any(ready() for ready in waiters))))

        # Process I/O until our completion condition is satisfied
        while not is_done():
            self._impl.ioloop.poll()
            self._impl.ioloop.process_timeouts()

        if self._closed_result.ready:
            try:
                if not isinstance(self._closed_result.value.error,
                                  exceptions.ConnectionClosedByClient):
                    LOGGER.error('Unexpected connection close detected: %r',
                                 self._closed_result.value.error)
                    raise self._closed_result.value.error
                LOGGER.info('User-initiated close: result=%r',
                            self._closed_result.value)
            finally:
                self._cleanup()

    def _request_channel_dispatch(self, channel_number: int) -> None:
        """Called by BlockingChannel instances to request a call to their
        _dispatch_events method or to terminate `process_data_events`;
        BlockingConnection will honor these requests from a safe context.

        :param channel_number: positive channel number to request a call
            to the channel's `_dispatch_events`; a negative channel number to
            request termination of `process_data_events`
        """
        self._channels_pending_dispatch.add(channel_number)

    def _dispatch_channel_events(self) -> None:
        """Invoke the `_dispatch_events` method on open channels that requested it."""
        if not self._channels_pending_dispatch:
            return

        with self._acquire_event_dispatch() as dispatch_acquired:
            if not dispatch_acquired:
                # Nested dispatch or dispatch blocked higher in call stack
                return

            candidates = list(self._channels_pending_dispatch)
            self._channels_pending_dispatch.clear()

            for channel_number in candidates:
                if channel_number < 0:
                    # This was meant to terminate process_data_events
                    continue

                try:
                    impl_channel = self._impl._channels[channel_number]
                except KeyError:
                    continue

                if impl_channel.is_open:
                    impl_channel._get_cookie()._dispatch_events()

    def _on_timer_ready(self, evt: _TimerEvt) -> None:
        """Handle expiry of a timer that was registered via
        `_adapter_call_later()`

        :param evt:

        """
        self._ready_events.append(evt)

    def _on_threadsafe_callback(self, user_callback: Callable[[],
                                                              None]) -> None:
        """Handle callback that was registered via
        `self._impl._adapter_add_callback_threadsafe`.

        :param user_callback: callback passed to our
            `add_callback_threadsafe` by the application.

        """
        # Turn it into a 0-delay timeout to take advantage of our existing logic
        # that deals with reentrancy
        self.call_later(0, user_callback)

    def _on_connection_blocked(
            self, user_callback: Callable[
                [pika.frame.Method[pika.spec.Connection.Blocked]],
                None], _impl: select_connection.SelectConnection,
            method_frame: pika.frame.Method[pika.spec.Connection.Blocked]
    ) -> None:
        """
        Handle Connection.Blocked notification from RabbitMQ broker.

        :param user_callback: callback passed to `add_on_connection_blocked_callback`
        :param _impl:
        :param method_frame: method frame having `method` member of type
            `pika.spec.Connection.Blocked`
        """
        self._ready_events.append(
            _ConnectionBlockedEvt(user_callback, method_frame))

    def _on_connection_unblocked(
        self, user_callback: Callable[
            [pika.frame.Method[pika.spec.Connection.Unblocked]],
            None], _impl: select_connection.SelectConnection,
        method_frame: pika.frame.Method[pika.spec.Connection.Unblocked]
    ) -> None:
        """
        Handle Connection.Unblocked notification from RabbitMQ broker.

        :param user_callback: callback passed to `add_on_connection_unblocked_callback`
        :param _impl:
        :param method_frame: method frame having `method` member of type
            `pika.spec.Connection.Blocked`
        """
        self._ready_events.append(
            _ConnectionUnblockedEvt(user_callback, method_frame))

    def _dispatch_connection_events(self) -> None:
        """Dispatch ready connection events."""
        if not self._ready_events:
            return

        with self._acquire_event_dispatch() as dispatch_acquired:
            if not dispatch_acquired:
                # Nested dispatch or dispatch blocked higher in call stack
                return

            # Limit dispatch to the number of currently ready events to avoid
            # getting stuck in this loop
            for _ in range(len(self._ready_events)):
                try:
                    evt = self._ready_events.popleft()
                except IndexError:
                    # Some events (e.g., timers) must have been cancelled
                    break

                evt.dispatch()

    def add_on_connection_blocked_callback(
        self, callback: Callable[[
            pika.connection.Connection, pika.frame.Method[pika.spec.Connection.
                                                          Blocked]
        ], None]
    ) -> None:
        """RabbitMQ AMQP extension - Add a callback to be notified when the
        connection gets blocked (`Connection.Blocked` received from RabbitMQ)
        due to the broker running low on resources (memory or disk). In this
        state RabbitMQ suspends processing incoming data until the connection
        is unblocked, so it's a good idea for publishers receiving this
        notification to suspend publishing until the connection becomes
        unblocked.

        NOTE: due to the blocking nature of BlockingConnection, if it's sending
        outbound data while the connection is/becomes blocked, the call may
        remain blocked until the connection becomes unblocked, if ever. You
        may use `ConnectionParameters.blocked_connection_timeout` to abort a
        BlockingConnection method call with an exception when the connection
        remains blocked longer than the given timeout value.

        See also `Connection.add_on_connection_unblocked_callback()`

        See also `ConnectionParameters.blocked_connection_timeout`.

        :param callback: Callback to call on `Connection.Blocked`,
            having the signature `callback(connection, pika.frame.Method)`,
            where connection is the `BlockingConnection` instance and the method
            frame's `method` member is of type `pika.spec.Connection.Blocked`

        """
        self._impl.add_on_connection_blocked_callback(
            functools.partial(
                self._on_connection_blocked,
                functools.partial(callback,
                                  cast(pika.connection.Connection, self))))

    def add_on_connection_unblocked_callback(
        self, callback: Callable[[
            pika.connection.Connection, pika.frame.Method[pika.spec.Connection.
                                                          Unblocked]
        ], None]
    ) -> None:
        """RabbitMQ AMQP extension - Add a callback to be notified when the
        connection gets unblocked (`Connection.Unblocked` frame is received from
        RabbitMQ) letting publishers know it's ok to start publishing again.

        :param callback: Callback to call on Connection.Unblocked`,
            having the signature `callback(connection, pika.frame.Method)`,
            where connection is the `BlockingConnection` instance and the method
            frame's `method` member is of type `pika.spec.Connection.Unblocked`

        """
        self._impl.add_on_connection_unblocked_callback(
            functools.partial(
                self._on_connection_unblocked,
                functools.partial(callback,
                                  cast(pika.connection.Connection, self))))

    def call_later(self, delay: float, callback: Callable[[], None]) -> int:
        """
        Create a single-shot timer to fire after delay seconds.

        Do not confuse with Tornado's timeout where you pass in the time you want to
        have your callback called. Only pass in the seconds until it's to be
        called.

        NOTE: the timer callbacks are dispatched only in the scope of
        specially-designated methods: see
        `BlockingConnection.process_data_events()` and
        `BlockingChannel.start_consuming()`.

        :param delay: The number of seconds to wait to call callback
        :param callback: The callback method with the signature
            callback()
        :returns: Opaque timer id
        """
        validators.require_callback(callback)

        evt = _TimerEvt(callback=callback)
        timer_id = cast(
            int,
            self._impl._adapter_call_later(
                delay, functools.partial(self._on_timer_ready, evt)))
        evt.timer_id = timer_id

        return timer_id

    def add_callback_threadsafe(self, callback: Callable[..., None]) -> None:
        """
        Requests a call to the given function as soon as possible in the context of this
        connection's thread.

        NOTE: This is the only thread-safe method in `BlockingConnection`. All
        other manipulations of `BlockingConnection` must be performed from the
        connection's thread.

        NOTE: the callbacks are dispatched only in the scope of
        specially-designated methods: see
        `BlockingConnection.process_data_events()` and
        `BlockingChannel.start_consuming()`.

        For example, a thread may request a call to the
        `BlockingChannel.basic_ack` method of a `BlockingConnection` that is
        running in a different thread via::

            connection.add_callback_threadsafe(
                functools.partial(channel.basic_ack, delivery_tag=...))

        NOTE: if you know that the requester is running on the same thread as
        the connection it is more efficient to use the
        `BlockingConnection.call_later()` method with a delay of 0.

        :param callback: The callback method; must be callable
        :raises pika.exceptions.ConnectionWrongStateError: if connection is
            closed
        """
        with self._cleanup_mutex:
            # NOTE: keep in mind that we may be called from another thread and
            # this mutex only synchronizes us with our connection cleanup logic,
            # so a simple check for "is_closed" is pretty much all we're allowed
            # to do here besides calling the only thread-safe method
            # _adapter_add_callback_threadsafe().
            if self.is_closed:
                raise exceptions.ConnectionWrongStateError(
                    'BlockingConnection.add_callback_threadsafe() called on '
                    'closed or closing connection.')

            self._impl._adapter_add_callback_threadsafe(
                functools.partial(self._on_threadsafe_callback, callback))

    def remove_timeout(self, timeout_id: int) -> None:
        """
        Remove a timer if it's still in the timeout stack.

        :param timeout_id: The opaque timer id to remove
        """
        # Remove from the impl's timeout stack
        self._impl._adapter_remove_timeout(timeout_id)

        # Remove from ready events, if the timer fired already
        for i, evt in enumerate(self._ready_events):
            if isinstance(evt, _TimerEvt) and evt.timer_id == timeout_id:
                index_to_remove = i
                break
        else:
            # Not found
            return

        del self._ready_events[index_to_remove]

    def update_secret(self, new_secret: str, reason: str) -> None:
        """RabbitMQ AMQP extension - This method updates the secret used to authenticate this connection.
        It is used when secrets have an expiration date and need to be renewed, like OAuth 2 tokens.

        :param new_secret: The new secret
        :param reason: The reason for the secret update

        :raises pika.exceptions.ConnectionWrongStateError: if connection is
            not open.
        """
        result = _CallbackResult()
        self._impl.update_secret(new_secret, reason, result.signal_once)
        self._flush_output(result.is_ready)

    def close(self,
              reply_code: int = 200,
              reply_text: str = 'Normal shutdown') -> None:
        """
        Disconnect from RabbitMQ.

        If there are any open channels, it will attempt to close them prior to fully disconnecting.
        Channels which have active consumers will attempt to send a Basic.Cancel to RabbitMQ to
        cleanly stop the delivery of messages prior to closing the channel.

        :param reply_code: The code number for the close
        :param reply_text: The text reason for the close
        :raises pika.exceptions.ConnectionWrongStateError: if called on a closed connection (NEW in
            v1.0.0)
        """
        if not self.is_open:
            msg = f'{self.__class__.__name__}.close({reply_code}, {reply_text!r}) called on closed connection.'
            LOGGER.error(msg)
            raise exceptions.ConnectionWrongStateError(msg)

        LOGGER.info('Closing connection (%s): %s', reply_code, reply_text)

        # Close channels that remain opened
        for impl_channel in list(self._impl._channels.values()):
            channel = impl_channel._get_cookie()
            if channel.is_open:
                try:
                    channel.close(reply_code, reply_text)
                except exceptions.ChannelClosed as exc:
                    # Log and suppress broker-closed channel
                    LOGGER.warning(
                        'Got ChannelClosed while closing channel '
                        'from connection.close: %r', exc)

        # Close the connection
        self._impl.close(reply_code, reply_text)

        self._flush_output(self._closed_result.is_ready)

    def process_data_events(self, time_limit: float | None = 0) -> None:
        """
        Will make sure that data events are processed.

        Dispatches timer and channel callbacks if not called from the scope of BlockingConnection or
        BlockingChannel callback. Your app can block on this method. If your application maintains a
        long-lived publisher connection, this method should be called periodically in order to
        respond to heartbeats and other data events. See `examples/long_running_publisher.py` for an
        example.

        :param time_limit: suggested upper bound on processing time in seconds. The actual blocking
            time depends on the granularity of the underlying ioloop. Zero means return as soon as
            possible. None means there is no limit on processing time and the function will block
            until I/O produces actionable events. Defaults to 0 for backward compatibility. This
            parameter is NEW in pika 0.10.0.
        """
        with self._acquire_event_dispatch() as dispatch_acquired:
            # Check if we can actually process pending events
            def common_terminator() -> bool:
                return bool(
                    dispatch_acquired and
                    (self._channels_pending_dispatch or self._ready_events))

            if time_limit is None:
                self._flush_output(common_terminator)
            else:
                with _IoloopTimerContext(time_limit, self._impl) as timer:
                    self._flush_output(timer.is_ready, common_terminator)

        if self._ready_events:
            self._dispatch_connection_events()

        if self._channels_pending_dispatch:
            self._dispatch_channel_events()

        if self._server_channel_closures:
            exc = self._server_channel_closures.popleft()
            LOGGER.debug('Channel closed by broker: %r', exc)
            raise exc

    def sleep(self, duration: float) -> None:
        """
        A safer way to sleep than calling time.sleep() directly that would keep the adapter from
        ignoring frames sent from the broker. The connection will "sleep" or block the number of
        seconds specified in duration in small intervals.

        :param duration: The time to sleep in seconds
        """
        assert duration >= 0, duration

        deadline = pika._utils.time_now() + duration
        time_limit = duration
        # Process events at least once
        while True:
            self.process_data_events(time_limit)
            time_limit = deadline - pika._utils.time_now()
            if time_limit <= 0:
                break

    def channel(self, channel_number: int | None = None) -> BlockingChannel:
        """
        Create a new channel with the next available channel number or pass in a channel number to
        use. Must be non-zero if you would like to specify but it is recommended that you let Pika
        manage the channel numbers.

        :param channel_number: optional channel number to use
        """
        with _CallbackResult(self._OnChannelOpenedArgs) as opened_args:
            impl_channel = self._impl.channel(
                channel_number=channel_number,
                on_open_callback=opened_args.set_value_once)

            # Create our proxy channel
            channel = BlockingChannel(impl_channel, self)

            # Link implementation channel with our proxy channel
            impl_channel._set_cookie(channel)

            # Drive I/O until Channel.Open-ok
            channel._flush_output(opened_args.is_ready)

        return channel

    #
    # Connections state properties
    #

    @property
    def is_closed(self) -> bool:
        """Returns a boolean reporting the current connection state."""
        return self._impl.is_closed

    @property
    def is_open(self) -> bool:
        """Returns a boolean reporting the current connection state."""
        return self._impl.is_open

    #
    # Properties that reflect server capabilities for the current connection
    #

    @property
    def accept_consumer_cancel_ok_supported(self) -> bool:
        """Specifies if the server accepts a client-sent basic.cancel-ok in reply to a broker-sent
        basic.cancel on the active connection.
        """
        return self._impl.accept_consumer_cancel_ok

    @property
    def basic_nack_supported(self) -> bool:
        """Specifies if the server supports basic.nack on the active connection."""
        return self._impl.basic_nack

    @property
    def consumer_cancel_notify_supported(self) -> bool:
        """Specifies if the server supports consumer cancel notification on the active
        connection.
        """
        return self._impl.consumer_cancel_notify

    @property
    def exchange_exchange_bindings_supported(self) -> bool:
        """Specifies if the active connection supports exchange to exchange bindings."""
        return self._impl.exchange_exchange_bindings

    @property
    def publisher_confirms_supported(self) -> bool:
        """Specifies if the active connection can use publisher confirmations."""
        return self._impl.publisher_confirms

    # Legacy property names for backward compatibility
    basic_nack = basic_nack_supported
    consumer_cancel_notify = consumer_cancel_notify_supported
    exchange_exchange_bindings = exchange_exchange_bindings_supported
    publisher_confirms = publisher_confirms_supported

accept_consumer_cancel_ok_supported property

accept_consumer_cancel_ok_supported: bool

Specifies if the server accepts a client-sent basic.cancel-ok in reply to a broker-sent basic.cancel on the active connection.

basic_nack_supported property

basic_nack_supported: bool

Specifies if the server supports basic.nack on the active connection.

consumer_cancel_notify_supported property

consumer_cancel_notify_supported: bool

Specifies if the server supports consumer cancel notification on the active connection.

exchange_exchange_bindings_supported property

exchange_exchange_bindings_supported: bool

Specifies if the active connection supports exchange to exchange bindings.

is_closed property

is_closed: bool

Returns a boolean reporting the current connection state.

is_open property

is_open: bool

Returns a boolean reporting the current connection state.

publisher_confirms_supported property

publisher_confirms_supported: bool

Specifies if the active connection can use publisher confirmations.

__init__

__init__(
    parameters: Parameters
    | Sequence[Parameters]
    | None = None,
    _impl_class: SelectConnection | None = None,
) -> None

Create a new instance of the Connection object.

PARAMETER DESCRIPTION
parameters

Connection parameters instance or non-empty sequence of them. If None, a pika.connection.Parameters instance will be created with default settings. See pika.AMQPConnectionWorkflow for more details about multiple parameter configurations and retries. Type: None | pika.connection.Parameters | sequence.

TYPE: Parameters | Sequence[Parameters] | None DEFAULT: None

_impl_class

for tests/debugging only; implementation class; None=default

TYPE: SelectConnection | None DEFAULT: None

RAISES DESCRIPTION
RuntimeError
Source code in pika/adapters/blocking_connection.py
def __init__(
        self,
        parameters: pika.connection.Parameters |
    Sequence[pika.connection.Parameters] | None = None,
        _impl_class: select_connection.SelectConnection | None = None
) -> None:
    """
    Create a new instance of the Connection object.

    :param parameters:
        Connection parameters instance or non-empty sequence of them. If
        None, a `pika.connection.Parameters` instance will be created with
        default settings. See `pika.AMQPConnectionWorkflow` for more
        details about multiple parameter configurations and retries.
        Type: `None | pika.connection.Parameters | sequence`.
    :param _impl_class: for tests/debugging only; implementation class;
        None=default

    :raises RuntimeError:
    """
    warnings.warn(
        'BlockingConnection is deprecated and will be removed in Pika 2.0. '
        'Use Connection instead, which runs its own IOLoop on a '
        'background thread and provides a thread-safe blocking API that '
        'does not stall heartbeats on slow message processing. See '
        'https://pika.github.io/pika/latest/modules/adapters/thread_safe/',
        DeprecationWarning,
        stacklevel=2,
    )
    # Used for mutual exclusion to avoid race condition between
    # BlockingConnection._cleanup() and another thread calling
    # BlockingConnection.add_callback_threadsafe() against a closed
    # ioloop.
    self._cleanup_mutex = threading.Lock()

    # Used by the _acquire_event_dispatch decorator; when already greater
    # than 0, event dispatch is already acquired higher up the call stack
    self._event_dispatch_suspend_depth = 0

    # Connection-specific events that are ready for dispatch: _TimerEvt,
    # _ConnectionBlockedEvt, _ConnectionUnblockedEvt
    self._ready_events: deque[Any] = deque()

    # Channel numbers of channels that are requesting a call to their
    # BlockingChannel._dispatch_events method; See
    # `_request_channel_dispatch`
    self._channels_pending_dispatch: set[int] = set()

    # Receives on_close_callback args from Connection
    self._closed_result = _CallbackResult(self._OnClosedArgs)

    # Store exceptions for server-initiated channel closures
    self._server_channel_closures: deque[Exception] = deque()

    # Declared without a value so that the type stays non-optional for the
    # public API, all of which runs only after a successful workflow.
    # `_create_connection()` may raise before this is ever assigned, so the
    # two members that can run on a half-built instance, `_cleanup()` and
    # `__repr__()`, read it with `getattr()`.
    self._impl: select_connection.SelectConnection

    self._impl = self._create_connection(parameters, _impl_class)
    self._impl.add_on_close_callback(self._closed_result.set_value_once)

add_callback_threadsafe

add_callback_threadsafe(
    callback: Callable[..., None],
) -> None

Requests a call to the given function as soon as possible in the context of this connection's thread.

NOTE: This is the only thread-safe method in BlockingConnection. All other manipulations of BlockingConnection must be performed from the connection's thread.

NOTE: the callbacks are dispatched only in the scope of specially-designated methods: see BlockingConnection.process_data_events() and BlockingChannel.start_consuming().

For example, a thread may request a call to the BlockingChannel.basic_ack method of a BlockingConnection that is running in a different thread via::

connection.add_callback_threadsafe(
    functools.partial(channel.basic_ack, delivery_tag=...))

NOTE: if you know that the requester is running on the same thread as the connection it is more efficient to use the BlockingConnection.call_later() method with a delay of 0.

PARAMETER DESCRIPTION
callback

The callback method; must be callable

TYPE: Callable[..., None]

RAISES DESCRIPTION
pika.exceptions.ConnectionWrongStateError

if connection is closed

Source code in pika/adapters/blocking_connection.py
def add_callback_threadsafe(self, callback: Callable[..., None]) -> None:
    """
    Requests a call to the given function as soon as possible in the context of this
    connection's thread.

    NOTE: This is the only thread-safe method in `BlockingConnection`. All
    other manipulations of `BlockingConnection` must be performed from the
    connection's thread.

    NOTE: the callbacks are dispatched only in the scope of
    specially-designated methods: see
    `BlockingConnection.process_data_events()` and
    `BlockingChannel.start_consuming()`.

    For example, a thread may request a call to the
    `BlockingChannel.basic_ack` method of a `BlockingConnection` that is
    running in a different thread via::

        connection.add_callback_threadsafe(
            functools.partial(channel.basic_ack, delivery_tag=...))

    NOTE: if you know that the requester is running on the same thread as
    the connection it is more efficient to use the
    `BlockingConnection.call_later()` method with a delay of 0.

    :param callback: The callback method; must be callable
    :raises pika.exceptions.ConnectionWrongStateError: if connection is
        closed
    """
    with self._cleanup_mutex:
        # NOTE: keep in mind that we may be called from another thread and
        # this mutex only synchronizes us with our connection cleanup logic,
        # so a simple check for "is_closed" is pretty much all we're allowed
        # to do here besides calling the only thread-safe method
        # _adapter_add_callback_threadsafe().
        if self.is_closed:
            raise exceptions.ConnectionWrongStateError(
                'BlockingConnection.add_callback_threadsafe() called on '
                'closed or closing connection.')

        self._impl._adapter_add_callback_threadsafe(
            functools.partial(self._on_threadsafe_callback, callback))

add_on_connection_blocked_callback

add_on_connection_blocked_callback(
    callback: Callable[[Connection, Method[Blocked]], None],
) -> None

RabbitMQ AMQP extension - Add a callback to be notified when the connection gets blocked (Connection.Blocked received from RabbitMQ) due to the broker running low on resources (memory or disk). In this state RabbitMQ suspends processing incoming data until the connection is unblocked, so it's a good idea for publishers receiving this notification to suspend publishing until the connection becomes unblocked.

NOTE: due to the blocking nature of BlockingConnection, if it's sending outbound data while the connection is/becomes blocked, the call may remain blocked until the connection becomes unblocked, if ever. You may use ConnectionParameters.blocked_connection_timeout to abort a BlockingConnection method call with an exception when the connection remains blocked longer than the given timeout value.

See also Connection.add_on_connection_unblocked_callback()

See also ConnectionParameters.blocked_connection_timeout.

PARAMETER DESCRIPTION
callback

Callback to call on Connection.Blocked, having the signature callback(connection, pika.frame.Method), where connection is the BlockingConnection instance and the method frame's method member is of type pika.spec.Connection.Blocked

TYPE: Callable[[Connection, Method[Blocked]], None]

Source code in pika/adapters/blocking_connection.py
def add_on_connection_blocked_callback(
    self, callback: Callable[[
        pika.connection.Connection, pika.frame.Method[pika.spec.Connection.
                                                      Blocked]
    ], None]
) -> None:
    """RabbitMQ AMQP extension - Add a callback to be notified when the
    connection gets blocked (`Connection.Blocked` received from RabbitMQ)
    due to the broker running low on resources (memory or disk). In this
    state RabbitMQ suspends processing incoming data until the connection
    is unblocked, so it's a good idea for publishers receiving this
    notification to suspend publishing until the connection becomes
    unblocked.

    NOTE: due to the blocking nature of BlockingConnection, if it's sending
    outbound data while the connection is/becomes blocked, the call may
    remain blocked until the connection becomes unblocked, if ever. You
    may use `ConnectionParameters.blocked_connection_timeout` to abort a
    BlockingConnection method call with an exception when the connection
    remains blocked longer than the given timeout value.

    See also `Connection.add_on_connection_unblocked_callback()`

    See also `ConnectionParameters.blocked_connection_timeout`.

    :param callback: Callback to call on `Connection.Blocked`,
        having the signature `callback(connection, pika.frame.Method)`,
        where connection is the `BlockingConnection` instance and the method
        frame's `method` member is of type `pika.spec.Connection.Blocked`

    """
    self._impl.add_on_connection_blocked_callback(
        functools.partial(
            self._on_connection_blocked,
            functools.partial(callback,
                              cast(pika.connection.Connection, self))))

add_on_connection_unblocked_callback

add_on_connection_unblocked_callback(
    callback: Callable[
        [Connection, Method[Unblocked]], None
    ],
) -> None

RabbitMQ AMQP extension - Add a callback to be notified when the connection gets unblocked (Connection.Unblocked frame is received from RabbitMQ) letting publishers know it's ok to start publishing again.

PARAMETER DESCRIPTION
callback

Callback to call on Connection.Unblocked, having the signaturecallback(connection, pika.frame.Method), where connection is theBlockingConnectioninstance and the method frame'smethodmember is of typepika.spec.Connection.Unblocked`

TYPE: Callable[[Connection, Method[Unblocked]], None]

Source code in pika/adapters/blocking_connection.py
def add_on_connection_unblocked_callback(
    self, callback: Callable[[
        pika.connection.Connection, pika.frame.Method[pika.spec.Connection.
                                                      Unblocked]
    ], None]
) -> None:
    """RabbitMQ AMQP extension - Add a callback to be notified when the
    connection gets unblocked (`Connection.Unblocked` frame is received from
    RabbitMQ) letting publishers know it's ok to start publishing again.

    :param callback: Callback to call on Connection.Unblocked`,
        having the signature `callback(connection, pika.frame.Method)`,
        where connection is the `BlockingConnection` instance and the method
        frame's `method` member is of type `pika.spec.Connection.Unblocked`

    """
    self._impl.add_on_connection_unblocked_callback(
        functools.partial(
            self._on_connection_unblocked,
            functools.partial(callback,
                              cast(pika.connection.Connection, self))))

call_later

call_later(
    delay: float, callback: Callable[[], None]
) -> int

Create a single-shot timer to fire after delay seconds.

Do not confuse with Tornado's timeout where you pass in the time you want to have your callback called. Only pass in the seconds until it's to be called.

NOTE: the timer callbacks are dispatched only in the scope of specially-designated methods: see BlockingConnection.process_data_events() and BlockingChannel.start_consuming().

PARAMETER DESCRIPTION
delay

The number of seconds to wait to call callback

TYPE: float

callback

The callback method with the signature callback()

TYPE: Callable[[], None]

RETURNS DESCRIPTION
int

Opaque timer id

Source code in pika/adapters/blocking_connection.py
def call_later(self, delay: float, callback: Callable[[], None]) -> int:
    """
    Create a single-shot timer to fire after delay seconds.

    Do not confuse with Tornado's timeout where you pass in the time you want to
    have your callback called. Only pass in the seconds until it's to be
    called.

    NOTE: the timer callbacks are dispatched only in the scope of
    specially-designated methods: see
    `BlockingConnection.process_data_events()` and
    `BlockingChannel.start_consuming()`.

    :param delay: The number of seconds to wait to call callback
    :param callback: The callback method with the signature
        callback()
    :returns: Opaque timer id
    """
    validators.require_callback(callback)

    evt = _TimerEvt(callback=callback)
    timer_id = cast(
        int,
        self._impl._adapter_call_later(
            delay, functools.partial(self._on_timer_ready, evt)))
    evt.timer_id = timer_id

    return timer_id

channel

channel(
    channel_number: int | None = None,
) -> BlockingChannel

Create a new channel with the next available channel number or pass in a channel number to use. Must be non-zero if you would like to specify but it is recommended that you let Pika manage the channel numbers.

PARAMETER DESCRIPTION
channel_number

optional channel number to use

TYPE: int | None DEFAULT: None

Source code in pika/adapters/blocking_connection.py
def channel(self, channel_number: int | None = None) -> BlockingChannel:
    """
    Create a new channel with the next available channel number or pass in a channel number to
    use. Must be non-zero if you would like to specify but it is recommended that you let Pika
    manage the channel numbers.

    :param channel_number: optional channel number to use
    """
    with _CallbackResult(self._OnChannelOpenedArgs) as opened_args:
        impl_channel = self._impl.channel(
            channel_number=channel_number,
            on_open_callback=opened_args.set_value_once)

        # Create our proxy channel
        channel = BlockingChannel(impl_channel, self)

        # Link implementation channel with our proxy channel
        impl_channel._set_cookie(channel)

        # Drive I/O until Channel.Open-ok
        channel._flush_output(opened_args.is_ready)

    return channel

close

close(
    reply_code: int = 200,
    reply_text: str = 'Normal shutdown',
) -> None

Disconnect from RabbitMQ.

If there are any open channels, it will attempt to close them prior to fully disconnecting. Channels which have active consumers will attempt to send a Basic.Cancel to RabbitMQ to cleanly stop the delivery of messages prior to closing the channel.

PARAMETER DESCRIPTION
reply_code

The code number for the close

TYPE: int DEFAULT: 200

reply_text

The text reason for the close

TYPE: str DEFAULT: 'Normal shutdown'

RAISES DESCRIPTION
pika.exceptions.ConnectionWrongStateError

if called on a closed connection (NEW in v1.0.0)

Source code in pika/adapters/blocking_connection.py
def close(self,
          reply_code: int = 200,
          reply_text: str = 'Normal shutdown') -> None:
    """
    Disconnect from RabbitMQ.

    If there are any open channels, it will attempt to close them prior to fully disconnecting.
    Channels which have active consumers will attempt to send a Basic.Cancel to RabbitMQ to
    cleanly stop the delivery of messages prior to closing the channel.

    :param reply_code: The code number for the close
    :param reply_text: The text reason for the close
    :raises pika.exceptions.ConnectionWrongStateError: if called on a closed connection (NEW in
        v1.0.0)
    """
    if not self.is_open:
        msg = f'{self.__class__.__name__}.close({reply_code}, {reply_text!r}) called on closed connection.'
        LOGGER.error(msg)
        raise exceptions.ConnectionWrongStateError(msg)

    LOGGER.info('Closing connection (%s): %s', reply_code, reply_text)

    # Close channels that remain opened
    for impl_channel in list(self._impl._channels.values()):
        channel = impl_channel._get_cookie()
        if channel.is_open:
            try:
                channel.close(reply_code, reply_text)
            except exceptions.ChannelClosed as exc:
                # Log and suppress broker-closed channel
                LOGGER.warning(
                    'Got ChannelClosed while closing channel '
                    'from connection.close: %r', exc)

    # Close the connection
    self._impl.close(reply_code, reply_text)

    self._flush_output(self._closed_result.is_ready)

process_data_events

process_data_events(time_limit: float | None = 0) -> None

Will make sure that data events are processed.

Dispatches timer and channel callbacks if not called from the scope of BlockingConnection or BlockingChannel callback. Your app can block on this method. If your application maintains a long-lived publisher connection, this method should be called periodically in order to respond to heartbeats and other data events. See examples/long_running_publisher.py for an example.

PARAMETER DESCRIPTION
time_limit

suggested upper bound on processing time in seconds. The actual blocking time depends on the granularity of the underlying ioloop. Zero means return as soon as possible. None means there is no limit on processing time and the function will block until I/O produces actionable events. Defaults to 0 for backward compatibility. This parameter is NEW in pika 0.10.0.

TYPE: float | None DEFAULT: 0

Source code in pika/adapters/blocking_connection.py
def process_data_events(self, time_limit: float | None = 0) -> None:
    """
    Will make sure that data events are processed.

    Dispatches timer and channel callbacks if not called from the scope of BlockingConnection or
    BlockingChannel callback. Your app can block on this method. If your application maintains a
    long-lived publisher connection, this method should be called periodically in order to
    respond to heartbeats and other data events. See `examples/long_running_publisher.py` for an
    example.

    :param time_limit: suggested upper bound on processing time in seconds. The actual blocking
        time depends on the granularity of the underlying ioloop. Zero means return as soon as
        possible. None means there is no limit on processing time and the function will block
        until I/O produces actionable events. Defaults to 0 for backward compatibility. This
        parameter is NEW in pika 0.10.0.
    """
    with self._acquire_event_dispatch() as dispatch_acquired:
        # Check if we can actually process pending events
        def common_terminator() -> bool:
            return bool(
                dispatch_acquired and
                (self._channels_pending_dispatch or self._ready_events))

        if time_limit is None:
            self._flush_output(common_terminator)
        else:
            with _IoloopTimerContext(time_limit, self._impl) as timer:
                self._flush_output(timer.is_ready, common_terminator)

    if self._ready_events:
        self._dispatch_connection_events()

    if self._channels_pending_dispatch:
        self._dispatch_channel_events()

    if self._server_channel_closures:
        exc = self._server_channel_closures.popleft()
        LOGGER.debug('Channel closed by broker: %r', exc)
        raise exc

remove_timeout

remove_timeout(timeout_id: int) -> None

Remove a timer if it's still in the timeout stack.

PARAMETER DESCRIPTION
timeout_id

The opaque timer id to remove

TYPE: int

Source code in pika/adapters/blocking_connection.py
def remove_timeout(self, timeout_id: int) -> None:
    """
    Remove a timer if it's still in the timeout stack.

    :param timeout_id: The opaque timer id to remove
    """
    # Remove from the impl's timeout stack
    self._impl._adapter_remove_timeout(timeout_id)

    # Remove from ready events, if the timer fired already
    for i, evt in enumerate(self._ready_events):
        if isinstance(evt, _TimerEvt) and evt.timer_id == timeout_id:
            index_to_remove = i
            break
    else:
        # Not found
        return

    del self._ready_events[index_to_remove]

sleep

sleep(duration: float) -> None

A safer way to sleep than calling time.sleep() directly that would keep the adapter from ignoring frames sent from the broker. The connection will "sleep" or block the number of seconds specified in duration in small intervals.

PARAMETER DESCRIPTION
duration

The time to sleep in seconds

TYPE: float

Source code in pika/adapters/blocking_connection.py
def sleep(self, duration: float) -> None:
    """
    A safer way to sleep than calling time.sleep() directly that would keep the adapter from
    ignoring frames sent from the broker. The connection will "sleep" or block the number of
    seconds specified in duration in small intervals.

    :param duration: The time to sleep in seconds
    """
    assert duration >= 0, duration

    deadline = pika._utils.time_now() + duration
    time_limit = duration
    # Process events at least once
    while True:
        self.process_data_events(time_limit)
        time_limit = deadline - pika._utils.time_now()
        if time_limit <= 0:
            break

update_secret

update_secret(new_secret: str, reason: str) -> None

RabbitMQ AMQP extension - This method updates the secret used to authenticate this connection. It is used when secrets have an expiration date and need to be renewed, like OAuth 2 tokens.

PARAMETER DESCRIPTION
new_secret

The new secret

TYPE: str

reason

The reason for the secret update

TYPE: str

RAISES DESCRIPTION
pika.exceptions.ConnectionWrongStateError

if connection is not open.

Source code in pika/adapters/blocking_connection.py
def update_secret(self, new_secret: str, reason: str) -> None:
    """RabbitMQ AMQP extension - This method updates the secret used to authenticate this connection.
    It is used when secrets have an expiration date and need to be renewed, like OAuth 2 tokens.

    :param new_secret: The new secret
    :param reason: The reason for the secret update

    :raises pika.exceptions.ConnectionWrongStateError: if connection is
        not open.
    """
    result = _CallbackResult()
    self._impl.update_secret(new_secret, reason, result.signal_once)
    self._flush_output(result.is_ready)

ReturnedMessage

Represents a message returned via Basic.Return in publish-acknowledgments mode.

Source code in pika/adapters/blocking_connection.py
class ReturnedMessage:
    """Represents a message returned via Basic.Return in publish-acknowledgments mode."""

    __slots__ = ('body', 'method', 'properties')

    def __init__(self, method: pika.spec.Basic.Return,
                 properties: pika.spec.BasicProperties, body: bytes) -> None:
        """
        :param method:
        :param properties: message properties
        :param body: message body; empty string if no body
        """
        self.method = method
        self.properties = properties
        self.body = body

__init__

__init__(
    method: Return, properties: BasicProperties, body: bytes
) -> None
PARAMETER DESCRIPTION
method

TYPE: Return

properties

message properties

TYPE: BasicProperties

body

message body; empty string if no body

TYPE: bytes

Source code in pika/adapters/blocking_connection.py
def __init__(self, method: pika.spec.Basic.Return,
             properties: pika.spec.BasicProperties, body: bytes) -> None:
    """
    :param method:
    :param properties: message properties
    :param body: message body; empty string if no body
    """
    self.method = method
    self.properties = properties
    self.body = body

Be sure to check out examples in examples.

Interacting with Pika from another thread

pika.BlockingConnection abstracts its I/O loop from the application and thus exposes pika.BlockingConnection.add_callback_threadsafe() to allow interacting with Pika from another thread.

Class Reference

BlockingConnection

The BlockingConnection creates a layer on top of Pika's asynchronous core providing methods that will block until their expected response has returned. Due to the asynchronous nature of the Basic.Deliver and Basic.Return calls from RabbitMQ to your application, you can still implement continuation-passing style asynchronous methods if you'd like to receive messages from RabbitMQ using :meth:basic_consume <BlockingChannel.basic_consume> or if you want to be notified of a delivery failure when using :meth:basic_publish <BlockingChannel.basic_publish>.

For more information about communicating with the blocking_connection adapter, be sure to check out the :class:BlockingChannel <BlockingChannel> class which implements the :class:Channel <pika.channel.Channel> based communication for the blocking_connection adapter.

To prevent recursion/reentrancy, the blocking connection and channel implementations queue asynchronously-delivered events received in nested context (e.g., while waiting for BlockingConnection.channel or BlockingChannel.queue_declare to complete), dispatching them synchronously once nesting returns to the desired context. This concerns all callbacks, such as those registered via BlockingConnection.call_later, BlockingConnection.add_on_connection_blocked_callback, BlockingConnection.add_on_connection_unblocked_callback, BlockingChannel.basic_consume, etc.

Blocked Connection deadlock avoidance: when RabbitMQ becomes low on resources, it emits Connection.Blocked (AMQP extension) to the client connection when client makes a resource-consuming request on that connection or its channel (e.g., Basic.Publish); subsequently, RabbitMQ suspsends processing requests from that connection until the affected resources are restored. See https://www.rabbitmq.com/connection-blocked.html. This may impact BlockingConnection and BlockingChannel operations in a way that users might not be expecting. For example, if the user dispatches BlockingChannel.basic_publish in non-publisher-confirmation mode while RabbitMQ is in this low-resource state followed by a synchronous request (e.g., BlockingConnection.channel, BlockingChannel.consume, BlockingChannel.basic_consume, etc.), the synchronous request will block indefinitely (until Connection.Unblocked) waiting for RabbitMQ to reply. If the blocked state persists for a long time, the blocking operation will appear to hang. In this state, BlockingConnection instance and its channels will not dispatch user callbacks. SOLUTION: To break this potential deadlock, applications may configure the blocked_connection_timeout connection parameter when instantiating BlockingConnection. Upon blocked connection timeout, this adapter will raise ConnectionBlockedTimeout exception. Seepika.connection.ConnectionParametersdocumentation to learn more about theblocked_connection_timeout` configuration.

Source code in pika/adapters/blocking_connection.py
class BlockingConnection:
    """The BlockingConnection creates a layer on top of Pika's asynchronous core
    providing methods that will block until their expected response has
    returned. Due to the asynchronous nature of the `Basic.Deliver` and
    `Basic.Return` calls from RabbitMQ to your application, you can still
    implement continuation-passing style asynchronous methods if you'd like to
    receive messages from RabbitMQ using
    :meth:`basic_consume <BlockingChannel.basic_consume>` or if you want to be
    notified of a delivery failure when using
    :meth:`basic_publish <BlockingChannel.basic_publish>`.

    For more information about communicating with the blocking_connection
    adapter, be sure to check out the
    :class:`BlockingChannel <BlockingChannel>` class which implements the
    :class:`Channel <pika.channel.Channel>` based communication for the
    blocking_connection adapter.

    To prevent recursion/reentrancy, the blocking connection and channel
    implementations queue asynchronously-delivered events received
    in nested context (e.g., while waiting for `BlockingConnection.channel` or
    `BlockingChannel.queue_declare` to complete), dispatching them synchronously
    once nesting returns to the desired context. This concerns all callbacks,
    such as those registered via `BlockingConnection.call_later`,
    `BlockingConnection.add_on_connection_blocked_callback`,
    `BlockingConnection.add_on_connection_unblocked_callback`,
    `BlockingChannel.basic_consume`, etc.

    Blocked Connection deadlock avoidance: when RabbitMQ becomes low on
    resources, it emits Connection.Blocked (AMQP extension) to the client
    connection when client makes a resource-consuming request on that connection
    or its channel (e.g., `Basic.Publish`); subsequently, RabbitMQ suspsends
    processing requests from that connection until the affected resources are
    restored. See https://www.rabbitmq.com/connection-blocked.html. This
    may impact `BlockingConnection` and `BlockingChannel` operations in a
    way that users might not be expecting. For example, if the user dispatches
    `BlockingChannel.basic_publish` in non-publisher-confirmation mode while
    RabbitMQ is in this low-resource state followed by a synchronous request
    (e.g., `BlockingConnection.channel`, `BlockingChannel.consume`,
    `BlockingChannel.basic_consume`, etc.), the synchronous request will block
    indefinitely (until Connection.Unblocked) waiting for RabbitMQ to reply. If
    the blocked state persists for a long time, the blocking operation will
    appear to hang. In this state, `BlockingConnection` instance and its
    channels will not dispatch user callbacks. SOLUTION: To break this potential
    deadlock, applications may configure the `blocked_connection_timeout`
    connection parameter when instantiating `BlockingConnection`. Upon blocked
    connection timeout, this adapter will raise ConnectionBlockedTimeout
    exception`. See `pika.connection.ConnectionParameters` documentation to
    learn more about the `blocked_connection_timeout` configuration.

    """

    class _OnClosedArgs(NamedTuple):
        connection: Any
        error: Any

    class _OnChannelOpenedArgs(NamedTuple):
        channel: Any

    def __init__(
            self,
            parameters: pika.connection.Parameters |
        Sequence[pika.connection.Parameters] | None = None,
            _impl_class: select_connection.SelectConnection | None = None
    ) -> None:
        """
        Create a new instance of the Connection object.

        :param parameters:
            Connection parameters instance or non-empty sequence of them. If
            None, a `pika.connection.Parameters` instance will be created with
            default settings. See `pika.AMQPConnectionWorkflow` for more
            details about multiple parameter configurations and retries.
            Type: `None | pika.connection.Parameters | sequence`.
        :param _impl_class: for tests/debugging only; implementation class;
            None=default

        :raises RuntimeError:
        """
        warnings.warn(
            'BlockingConnection is deprecated and will be removed in Pika 2.0. '
            'Use Connection instead, which runs its own IOLoop on a '
            'background thread and provides a thread-safe blocking API that '
            'does not stall heartbeats on slow message processing. See '
            'https://pika.github.io/pika/latest/modules/adapters/thread_safe/',
            DeprecationWarning,
            stacklevel=2,
        )
        # Used for mutual exclusion to avoid race condition between
        # BlockingConnection._cleanup() and another thread calling
        # BlockingConnection.add_callback_threadsafe() against a closed
        # ioloop.
        self._cleanup_mutex = threading.Lock()

        # Used by the _acquire_event_dispatch decorator; when already greater
        # than 0, event dispatch is already acquired higher up the call stack
        self._event_dispatch_suspend_depth = 0

        # Connection-specific events that are ready for dispatch: _TimerEvt,
        # _ConnectionBlockedEvt, _ConnectionUnblockedEvt
        self._ready_events: deque[Any] = deque()

        # Channel numbers of channels that are requesting a call to their
        # BlockingChannel._dispatch_events method; See
        # `_request_channel_dispatch`
        self._channels_pending_dispatch: set[int] = set()

        # Receives on_close_callback args from Connection
        self._closed_result = _CallbackResult(self._OnClosedArgs)

        # Store exceptions for server-initiated channel closures
        self._server_channel_closures: deque[Exception] = deque()

        # Declared without a value so that the type stays non-optional for the
        # public API, all of which runs only after a successful workflow.
        # `_create_connection()` may raise before this is ever assigned, so the
        # two members that can run on a half-built instance, `_cleanup()` and
        # `__repr__()`, read it with `getattr()`.
        self._impl: select_connection.SelectConnection

        self._impl = self._create_connection(parameters, _impl_class)
        self._impl.add_on_close_callback(self._closed_result.set_value_once)

    @override
    def __repr__(self) -> str:
        # `getattr` because `__repr__` runs on a half-built instance whenever
        # `__init__` raises: a traceback or debugger rendering the frame's locals
        # must not turn the real error into an `AttributeError`.
        impl = getattr(self, '_impl', None)
        return f'<{self.__class__.__name__} impl={impl!r}>'

    def __enter__(self) -> Self:
        # Prepare `with` context
        return self

    def __exit__(self, exc_type: type[BaseException] | None,
                 value: BaseException | None,
                 traceback: TracebackType | None) -> None:
        # Close connection after `with` context
        if self.is_open:
            self.close()

    def _cleanup(self) -> None:
        """Clean up members that might inhibit garbage collection."""
        with self._cleanup_mutex:
            # `getattr` rather than a plain attribute test: `__init__` may have
            # raised before `_impl` was assigned.
            if getattr(self, '_impl', None) is not None:
                self._impl.ioloop.close()
            self._ready_events.clear()
            self._closed_result.reset()

    @contextlib.contextmanager
    def _acquire_event_dispatch(self) -> Generator[bool, Any, None]:
        """
        Context manager that controls access to event dispatcher for preventing reentrancy.

        The "as" value is True if the managed code block owns the event dispatcher and False if
        caller higher up in the call stack already owns it. Only managed code that gets ownership
        (got True) is permitted to dispatch
        """
        try:
            # __enter__ part
            self._event_dispatch_suspend_depth += 1
            yield self._event_dispatch_suspend_depth == 1
        finally:
            # __exit__ part
            self._event_dispatch_suspend_depth -= 1

    def _create_connection(
        self,
        configs: pika.connection.Parameters |
        Sequence[pika.connection.Parameters] | None = None,
        impl_class: select_connection.SelectConnection | None = None
    ) -> select_connection.SelectConnection:
        """
        Run connection workflow, blocking until it completes.

        :param configs: Connection
            parameters instance or non-empty sequence of them.
            Type: `None | pika.connection.Parameters | sequence`.
        :param impl_class: for tests/debugging only; implementation class.


        :raises Exception: on failure
        """
        if configs is None:
            configs = (pika.connection.Parameters(),)

        if isinstance(configs, pika.connection.Parameters):
            configs = (configs,)

        if not configs:
            raise ValueError('Expected a non-empty sequence of connection '
                             f'parameters, but got {configs!r}.')

        # Connection workflow completion args
        #   `result` may be an instance of connection on success or exception on
        #   failure.
        on_cw_done_result = _CallbackResult(
            namedtuple('BlockingConnection_OnConnectionWorkflowDoneArgs',
                       'result'))

        resolved_impl_class = impl_class or select_connection.SelectConnection

        ioloop = select_connection.IOLoop()

        ioloop.activate_poller()
        try:
            resolved_impl_class.create_connection(
                configs,
                on_done=on_cw_done_result.set_value_once,
                custom_ioloop=ioloop)

            while not on_cw_done_result.ready:
                ioloop.poll()
                ioloop.process_timeouts()

            if isinstance(on_cw_done_result.value.result, BaseException):
                error = on_cw_done_result.value.result
                LOGGER.error('Connection workflow failed: %r', error)
                raise self._reap_last_connection_workflow_error(error)
            LOGGER.info('Connection workflow succeeded: %r',
                        on_cw_done_result.value.result)
            return on_cw_done_result.value.result
        except Exception:
            LOGGER.exception('Error in _create_connection().')
            ioloop.close()
            self._cleanup()
            raise

    @staticmethod
    def _reap_last_connection_workflow_error(
            error: BaseException) -> BaseException:
        """
        Extract exception value from the last connection attempt.

        :param error: error passed by the `AMQPConnectionWorkflow` completion callback.
        :returns: Exception value from the last connection attempt
        """
        if isinstance(error, connection_workflow.AMQPConnectionWorkflowFailed):
            # Extract exception value from the last connection attempt
            last = error.exceptions[-1]
            if isinstance(last,
                          connection_workflow.AMQPConnectorSocketConnectError):
                error = exceptions.AMQPConnectionError(last,
                                                       host=last.host,
                                                       port=last.port)
            elif isinstance(last,
                            connection_workflow.AMQPConnectorPhaseErrorBase):
                error = last.exception
            else:
                error = last

        return error

    def _flush_output(self, *waiters: Callable[[], bool]) -> None:
        """
        Flush output and process input while waiting for any of the given callbacks to return true.
        The wait is aborted upon connection-close. Otherwise, processing continues until the output
        is flushed AND at least one of the callbacks returns true. If there are no callbacks, then
        processing ends when all output is flushed.

        :param waiters: sequence of zero or more callables taking no args and returning true when
            it's time to stop processing. Their results are OR'ed together.
        :raises Exception: exceptions passed by impl if opening of connection fails or connection
            closes.
        """
        if self.is_closed:
            raise exceptions.ConnectionWrongStateError()

        # Conditions for terminating the processing loop:
        #   connection closed
        #         OR
        #   empty outbound buffer and no waiters
        #         OR
        #   empty outbound buffer and any waiter is ready
        def is_done() -> bool:
            return (self._closed_result.ready or
                    ((not self._impl._transport or
                      self._impl._get_write_buffer_size() == 0) and
                     (not waiters or any(ready() for ready in waiters))))

        # Process I/O until our completion condition is satisfied
        while not is_done():
            self._impl.ioloop.poll()
            self._impl.ioloop.process_timeouts()

        if self._closed_result.ready:
            try:
                if not isinstance(self._closed_result.value.error,
                                  exceptions.ConnectionClosedByClient):
                    LOGGER.error('Unexpected connection close detected: %r',
                                 self._closed_result.value.error)
                    raise self._closed_result.value.error
                LOGGER.info('User-initiated close: result=%r',
                            self._closed_result.value)
            finally:
                self._cleanup()

    def _request_channel_dispatch(self, channel_number: int) -> None:
        """Called by BlockingChannel instances to request a call to their
        _dispatch_events method or to terminate `process_data_events`;
        BlockingConnection will honor these requests from a safe context.

        :param channel_number: positive channel number to request a call
            to the channel's `_dispatch_events`; a negative channel number to
            request termination of `process_data_events`
        """
        self._channels_pending_dispatch.add(channel_number)

    def _dispatch_channel_events(self) -> None:
        """Invoke the `_dispatch_events` method on open channels that requested it."""
        if not self._channels_pending_dispatch:
            return

        with self._acquire_event_dispatch() as dispatch_acquired:
            if not dispatch_acquired:
                # Nested dispatch or dispatch blocked higher in call stack
                return

            candidates = list(self._channels_pending_dispatch)
            self._channels_pending_dispatch.clear()

            for channel_number in candidates:
                if channel_number < 0:
                    # This was meant to terminate process_data_events
                    continue

                try:
                    impl_channel = self._impl._channels[channel_number]
                except KeyError:
                    continue

                if impl_channel.is_open:
                    impl_channel._get_cookie()._dispatch_events()

    def _on_timer_ready(self, evt: _TimerEvt) -> None:
        """Handle expiry of a timer that was registered via
        `_adapter_call_later()`

        :param evt:

        """
        self._ready_events.append(evt)

    def _on_threadsafe_callback(self, user_callback: Callable[[],
                                                              None]) -> None:
        """Handle callback that was registered via
        `self._impl._adapter_add_callback_threadsafe`.

        :param user_callback: callback passed to our
            `add_callback_threadsafe` by the application.

        """
        # Turn it into a 0-delay timeout to take advantage of our existing logic
        # that deals with reentrancy
        self.call_later(0, user_callback)

    def _on_connection_blocked(
            self, user_callback: Callable[
                [pika.frame.Method[pika.spec.Connection.Blocked]],
                None], _impl: select_connection.SelectConnection,
            method_frame: pika.frame.Method[pika.spec.Connection.Blocked]
    ) -> None:
        """
        Handle Connection.Blocked notification from RabbitMQ broker.

        :param user_callback: callback passed to `add_on_connection_blocked_callback`
        :param _impl:
        :param method_frame: method frame having `method` member of type
            `pika.spec.Connection.Blocked`
        """
        self._ready_events.append(
            _ConnectionBlockedEvt(user_callback, method_frame))

    def _on_connection_unblocked(
        self, user_callback: Callable[
            [pika.frame.Method[pika.spec.Connection.Unblocked]],
            None], _impl: select_connection.SelectConnection,
        method_frame: pika.frame.Method[pika.spec.Connection.Unblocked]
    ) -> None:
        """
        Handle Connection.Unblocked notification from RabbitMQ broker.

        :param user_callback: callback passed to `add_on_connection_unblocked_callback`
        :param _impl:
        :param method_frame: method frame having `method` member of type
            `pika.spec.Connection.Blocked`
        """
        self._ready_events.append(
            _ConnectionUnblockedEvt(user_callback, method_frame))

    def _dispatch_connection_events(self) -> None:
        """Dispatch ready connection events."""
        if not self._ready_events:
            return

        with self._acquire_event_dispatch() as dispatch_acquired:
            if not dispatch_acquired:
                # Nested dispatch or dispatch blocked higher in call stack
                return

            # Limit dispatch to the number of currently ready events to avoid
            # getting stuck in this loop
            for _ in range(len(self._ready_events)):
                try:
                    evt = self._ready_events.popleft()
                except IndexError:
                    # Some events (e.g., timers) must have been cancelled
                    break

                evt.dispatch()

    def add_on_connection_blocked_callback(
        self, callback: Callable[[
            pika.connection.Connection, pika.frame.Method[pika.spec.Connection.
                                                          Blocked]
        ], None]
    ) -> None:
        """RabbitMQ AMQP extension - Add a callback to be notified when the
        connection gets blocked (`Connection.Blocked` received from RabbitMQ)
        due to the broker running low on resources (memory or disk). In this
        state RabbitMQ suspends processing incoming data until the connection
        is unblocked, so it's a good idea for publishers receiving this
        notification to suspend publishing until the connection becomes
        unblocked.

        NOTE: due to the blocking nature of BlockingConnection, if it's sending
        outbound data while the connection is/becomes blocked, the call may
        remain blocked until the connection becomes unblocked, if ever. You
        may use `ConnectionParameters.blocked_connection_timeout` to abort a
        BlockingConnection method call with an exception when the connection
        remains blocked longer than the given timeout value.

        See also `Connection.add_on_connection_unblocked_callback()`

        See also `ConnectionParameters.blocked_connection_timeout`.

        :param callback: Callback to call on `Connection.Blocked`,
            having the signature `callback(connection, pika.frame.Method)`,
            where connection is the `BlockingConnection` instance and the method
            frame's `method` member is of type `pika.spec.Connection.Blocked`

        """
        self._impl.add_on_connection_blocked_callback(
            functools.partial(
                self._on_connection_blocked,
                functools.partial(callback,
                                  cast(pika.connection.Connection, self))))

    def add_on_connection_unblocked_callback(
        self, callback: Callable[[
            pika.connection.Connection, pika.frame.Method[pika.spec.Connection.
                                                          Unblocked]
        ], None]
    ) -> None:
        """RabbitMQ AMQP extension - Add a callback to be notified when the
        connection gets unblocked (`Connection.Unblocked` frame is received from
        RabbitMQ) letting publishers know it's ok to start publishing again.

        :param callback: Callback to call on Connection.Unblocked`,
            having the signature `callback(connection, pika.frame.Method)`,
            where connection is the `BlockingConnection` instance and the method
            frame's `method` member is of type `pika.spec.Connection.Unblocked`

        """
        self._impl.add_on_connection_unblocked_callback(
            functools.partial(
                self._on_connection_unblocked,
                functools.partial(callback,
                                  cast(pika.connection.Connection, self))))

    def call_later(self, delay: float, callback: Callable[[], None]) -> int:
        """
        Create a single-shot timer to fire after delay seconds.

        Do not confuse with Tornado's timeout where you pass in the time you want to
        have your callback called. Only pass in the seconds until it's to be
        called.

        NOTE: the timer callbacks are dispatched only in the scope of
        specially-designated methods: see
        `BlockingConnection.process_data_events()` and
        `BlockingChannel.start_consuming()`.

        :param delay: The number of seconds to wait to call callback
        :param callback: The callback method with the signature
            callback()
        :returns: Opaque timer id
        """
        validators.require_callback(callback)

        evt = _TimerEvt(callback=callback)
        timer_id = cast(
            int,
            self._impl._adapter_call_later(
                delay, functools.partial(self._on_timer_ready, evt)))
        evt.timer_id = timer_id

        return timer_id

    def add_callback_threadsafe(self, callback: Callable[..., None]) -> None:
        """
        Requests a call to the given function as soon as possible in the context of this
        connection's thread.

        NOTE: This is the only thread-safe method in `BlockingConnection`. All
        other manipulations of `BlockingConnection` must be performed from the
        connection's thread.

        NOTE: the callbacks are dispatched only in the scope of
        specially-designated methods: see
        `BlockingConnection.process_data_events()` and
        `BlockingChannel.start_consuming()`.

        For example, a thread may request a call to the
        `BlockingChannel.basic_ack` method of a `BlockingConnection` that is
        running in a different thread via::

            connection.add_callback_threadsafe(
                functools.partial(channel.basic_ack, delivery_tag=...))

        NOTE: if you know that the requester is running on the same thread as
        the connection it is more efficient to use the
        `BlockingConnection.call_later()` method with a delay of 0.

        :param callback: The callback method; must be callable
        :raises pika.exceptions.ConnectionWrongStateError: if connection is
            closed
        """
        with self._cleanup_mutex:
            # NOTE: keep in mind that we may be called from another thread and
            # this mutex only synchronizes us with our connection cleanup logic,
            # so a simple check for "is_closed" is pretty much all we're allowed
            # to do here besides calling the only thread-safe method
            # _adapter_add_callback_threadsafe().
            if self.is_closed:
                raise exceptions.ConnectionWrongStateError(
                    'BlockingConnection.add_callback_threadsafe() called on '
                    'closed or closing connection.')

            self._impl._adapter_add_callback_threadsafe(
                functools.partial(self._on_threadsafe_callback, callback))

    def remove_timeout(self, timeout_id: int) -> None:
        """
        Remove a timer if it's still in the timeout stack.

        :param timeout_id: The opaque timer id to remove
        """
        # Remove from the impl's timeout stack
        self._impl._adapter_remove_timeout(timeout_id)

        # Remove from ready events, if the timer fired already
        for i, evt in enumerate(self._ready_events):
            if isinstance(evt, _TimerEvt) and evt.timer_id == timeout_id:
                index_to_remove = i
                break
        else:
            # Not found
            return

        del self._ready_events[index_to_remove]

    def update_secret(self, new_secret: str, reason: str) -> None:
        """RabbitMQ AMQP extension - This method updates the secret used to authenticate this connection.
        It is used when secrets have an expiration date and need to be renewed, like OAuth 2 tokens.

        :param new_secret: The new secret
        :param reason: The reason for the secret update

        :raises pika.exceptions.ConnectionWrongStateError: if connection is
            not open.
        """
        result = _CallbackResult()
        self._impl.update_secret(new_secret, reason, result.signal_once)
        self._flush_output(result.is_ready)

    def close(self,
              reply_code: int = 200,
              reply_text: str = 'Normal shutdown') -> None:
        """
        Disconnect from RabbitMQ.

        If there are any open channels, it will attempt to close them prior to fully disconnecting.
        Channels which have active consumers will attempt to send a Basic.Cancel to RabbitMQ to
        cleanly stop the delivery of messages prior to closing the channel.

        :param reply_code: The code number for the close
        :param reply_text: The text reason for the close
        :raises pika.exceptions.ConnectionWrongStateError: if called on a closed connection (NEW in
            v1.0.0)
        """
        if not self.is_open:
            msg = f'{self.__class__.__name__}.close({reply_code}, {reply_text!r}) called on closed connection.'
            LOGGER.error(msg)
            raise exceptions.ConnectionWrongStateError(msg)

        LOGGER.info('Closing connection (%s): %s', reply_code, reply_text)

        # Close channels that remain opened
        for impl_channel in list(self._impl._channels.values()):
            channel = impl_channel._get_cookie()
            if channel.is_open:
                try:
                    channel.close(reply_code, reply_text)
                except exceptions.ChannelClosed as exc:
                    # Log and suppress broker-closed channel
                    LOGGER.warning(
                        'Got ChannelClosed while closing channel '
                        'from connection.close: %r', exc)

        # Close the connection
        self._impl.close(reply_code, reply_text)

        self._flush_output(self._closed_result.is_ready)

    def process_data_events(self, time_limit: float | None = 0) -> None:
        """
        Will make sure that data events are processed.

        Dispatches timer and channel callbacks if not called from the scope of BlockingConnection or
        BlockingChannel callback. Your app can block on this method. If your application maintains a
        long-lived publisher connection, this method should be called periodically in order to
        respond to heartbeats and other data events. See `examples/long_running_publisher.py` for an
        example.

        :param time_limit: suggested upper bound on processing time in seconds. The actual blocking
            time depends on the granularity of the underlying ioloop. Zero means return as soon as
            possible. None means there is no limit on processing time and the function will block
            until I/O produces actionable events. Defaults to 0 for backward compatibility. This
            parameter is NEW in pika 0.10.0.
        """
        with self._acquire_event_dispatch() as dispatch_acquired:
            # Check if we can actually process pending events
            def common_terminator() -> bool:
                return bool(
                    dispatch_acquired and
                    (self._channels_pending_dispatch or self._ready_events))

            if time_limit is None:
                self._flush_output(common_terminator)
            else:
                with _IoloopTimerContext(time_limit, self._impl) as timer:
                    self._flush_output(timer.is_ready, common_terminator)

        if self._ready_events:
            self._dispatch_connection_events()

        if self._channels_pending_dispatch:
            self._dispatch_channel_events()

        if self._server_channel_closures:
            exc = self._server_channel_closures.popleft()
            LOGGER.debug('Channel closed by broker: %r', exc)
            raise exc

    def sleep(self, duration: float) -> None:
        """
        A safer way to sleep than calling time.sleep() directly that would keep the adapter from
        ignoring frames sent from the broker. The connection will "sleep" or block the number of
        seconds specified in duration in small intervals.

        :param duration: The time to sleep in seconds
        """
        assert duration >= 0, duration

        deadline = pika._utils.time_now() + duration
        time_limit = duration
        # Process events at least once
        while True:
            self.process_data_events(time_limit)
            time_limit = deadline - pika._utils.time_now()
            if time_limit <= 0:
                break

    def channel(self, channel_number: int | None = None) -> BlockingChannel:
        """
        Create a new channel with the next available channel number or pass in a channel number to
        use. Must be non-zero if you would like to specify but it is recommended that you let Pika
        manage the channel numbers.

        :param channel_number: optional channel number to use
        """
        with _CallbackResult(self._OnChannelOpenedArgs) as opened_args:
            impl_channel = self._impl.channel(
                channel_number=channel_number,
                on_open_callback=opened_args.set_value_once)

            # Create our proxy channel
            channel = BlockingChannel(impl_channel, self)

            # Link implementation channel with our proxy channel
            impl_channel._set_cookie(channel)

            # Drive I/O until Channel.Open-ok
            channel._flush_output(opened_args.is_ready)

        return channel

    #
    # Connections state properties
    #

    @property
    def is_closed(self) -> bool:
        """Returns a boolean reporting the current connection state."""
        return self._impl.is_closed

    @property
    def is_open(self) -> bool:
        """Returns a boolean reporting the current connection state."""
        return self._impl.is_open

    #
    # Properties that reflect server capabilities for the current connection
    #

    @property
    def accept_consumer_cancel_ok_supported(self) -> bool:
        """Specifies if the server accepts a client-sent basic.cancel-ok in reply to a broker-sent
        basic.cancel on the active connection.
        """
        return self._impl.accept_consumer_cancel_ok

    @property
    def basic_nack_supported(self) -> bool:
        """Specifies if the server supports basic.nack on the active connection."""
        return self._impl.basic_nack

    @property
    def consumer_cancel_notify_supported(self) -> bool:
        """Specifies if the server supports consumer cancel notification on the active
        connection.
        """
        return self._impl.consumer_cancel_notify

    @property
    def exchange_exchange_bindings_supported(self) -> bool:
        """Specifies if the active connection supports exchange to exchange bindings."""
        return self._impl.exchange_exchange_bindings

    @property
    def publisher_confirms_supported(self) -> bool:
        """Specifies if the active connection can use publisher confirmations."""
        return self._impl.publisher_confirms

    # Legacy property names for backward compatibility
    basic_nack = basic_nack_supported
    consumer_cancel_notify = consumer_cancel_notify_supported
    exchange_exchange_bindings = exchange_exchange_bindings_supported
    publisher_confirms = publisher_confirms_supported

_channels_pending_dispatch instance-attribute

_channels_pending_dispatch: set[int] = set()

_cleanup_mutex instance-attribute

_cleanup_mutex = threading.Lock()

_closed_result instance-attribute

_closed_result = _CallbackResult(self._OnClosedArgs)

_event_dispatch_suspend_depth instance-attribute

_event_dispatch_suspend_depth = 0

_impl instance-attribute

_impl: SelectConnection = self._create_connection(
    parameters, _impl_class
)

_ready_events instance-attribute

_ready_events: deque[Any] = deque()

_server_channel_closures instance-attribute

_server_channel_closures: deque[Exception] = deque()

accept_consumer_cancel_ok_supported property

accept_consumer_cancel_ok_supported: bool

Specifies if the server accepts a client-sent basic.cancel-ok in reply to a broker-sent basic.cancel on the active connection.

basic_nack class-attribute instance-attribute

basic_nack = basic_nack_supported

basic_nack_supported property

basic_nack_supported: bool

Specifies if the server supports basic.nack on the active connection.

consumer_cancel_notify class-attribute instance-attribute

consumer_cancel_notify = consumer_cancel_notify_supported

consumer_cancel_notify_supported property

consumer_cancel_notify_supported: bool

Specifies if the server supports consumer cancel notification on the active connection.

exchange_exchange_bindings class-attribute instance-attribute

exchange_exchange_bindings = (
    exchange_exchange_bindings_supported
)

exchange_exchange_bindings_supported property

exchange_exchange_bindings_supported: bool

Specifies if the active connection supports exchange to exchange bindings.

is_closed property

is_closed: bool

Returns a boolean reporting the current connection state.

is_open property

is_open: bool

Returns a boolean reporting the current connection state.

publisher_confirms class-attribute instance-attribute

publisher_confirms = publisher_confirms_supported

publisher_confirms_supported property

publisher_confirms_supported: bool

Specifies if the active connection can use publisher confirmations.

_OnChannelOpenedArgs

Bases: NamedTuple

Source code in pika/adapters/blocking_connection.py
class _OnChannelOpenedArgs(NamedTuple):
    channel: Any

_OnClosedArgs

Bases: NamedTuple

Source code in pika/adapters/blocking_connection.py
class _OnClosedArgs(NamedTuple):
    connection: Any
    error: Any

__enter__

__enter__() -> Self
Source code in pika/adapters/blocking_connection.py
def __enter__(self) -> Self:
    # Prepare `with` context
    return self

__exit__

__exit__(
    exc_type: type[BaseException] | None,
    value: BaseException | None,
    traceback: TracebackType | None,
) -> None
Source code in pika/adapters/blocking_connection.py
def __exit__(self, exc_type: type[BaseException] | None,
             value: BaseException | None,
             traceback: TracebackType | None) -> None:
    # Close connection after `with` context
    if self.is_open:
        self.close()

__init__

__init__(
    parameters: Parameters
    | Sequence[Parameters]
    | None = None,
    _impl_class: SelectConnection | None = None,
) -> None

Create a new instance of the Connection object.

PARAMETER DESCRIPTION
parameters

Connection parameters instance or non-empty sequence of them. If None, a pika.connection.Parameters instance will be created with default settings. See pika.AMQPConnectionWorkflow for more details about multiple parameter configurations and retries. Type: None | pika.connection.Parameters | sequence.

TYPE: Parameters | Sequence[Parameters] | None DEFAULT: None

_impl_class

for tests/debugging only; implementation class; None=default

TYPE: SelectConnection | None DEFAULT: None

RAISES DESCRIPTION
RuntimeError
Source code in pika/adapters/blocking_connection.py
def __init__(
        self,
        parameters: pika.connection.Parameters |
    Sequence[pika.connection.Parameters] | None = None,
        _impl_class: select_connection.SelectConnection | None = None
) -> None:
    """
    Create a new instance of the Connection object.

    :param parameters:
        Connection parameters instance or non-empty sequence of them. If
        None, a `pika.connection.Parameters` instance will be created with
        default settings. See `pika.AMQPConnectionWorkflow` for more
        details about multiple parameter configurations and retries.
        Type: `None | pika.connection.Parameters | sequence`.
    :param _impl_class: for tests/debugging only; implementation class;
        None=default

    :raises RuntimeError:
    """
    warnings.warn(
        'BlockingConnection is deprecated and will be removed in Pika 2.0. '
        'Use Connection instead, which runs its own IOLoop on a '
        'background thread and provides a thread-safe blocking API that '
        'does not stall heartbeats on slow message processing. See '
        'https://pika.github.io/pika/latest/modules/adapters/thread_safe/',
        DeprecationWarning,
        stacklevel=2,
    )
    # Used for mutual exclusion to avoid race condition between
    # BlockingConnection._cleanup() and another thread calling
    # BlockingConnection.add_callback_threadsafe() against a closed
    # ioloop.
    self._cleanup_mutex = threading.Lock()

    # Used by the _acquire_event_dispatch decorator; when already greater
    # than 0, event dispatch is already acquired higher up the call stack
    self._event_dispatch_suspend_depth = 0

    # Connection-specific events that are ready for dispatch: _TimerEvt,
    # _ConnectionBlockedEvt, _ConnectionUnblockedEvt
    self._ready_events: deque[Any] = deque()

    # Channel numbers of channels that are requesting a call to their
    # BlockingChannel._dispatch_events method; See
    # `_request_channel_dispatch`
    self._channels_pending_dispatch: set[int] = set()

    # Receives on_close_callback args from Connection
    self._closed_result = _CallbackResult(self._OnClosedArgs)

    # Store exceptions for server-initiated channel closures
    self._server_channel_closures: deque[Exception] = deque()

    # Declared without a value so that the type stays non-optional for the
    # public API, all of which runs only after a successful workflow.
    # `_create_connection()` may raise before this is ever assigned, so the
    # two members that can run on a half-built instance, `_cleanup()` and
    # `__repr__()`, read it with `getattr()`.
    self._impl: select_connection.SelectConnection

    self._impl = self._create_connection(parameters, _impl_class)
    self._impl.add_on_close_callback(self._closed_result.set_value_once)

__repr__

__repr__() -> str
Source code in pika/adapters/blocking_connection.py
@override
def __repr__(self) -> str:
    # `getattr` because `__repr__` runs on a half-built instance whenever
    # `__init__` raises: a traceback or debugger rendering the frame's locals
    # must not turn the real error into an `AttributeError`.
    impl = getattr(self, '_impl', None)
    return f'<{self.__class__.__name__} impl={impl!r}>'

_acquire_event_dispatch

_acquire_event_dispatch() -> Generator[bool, Any, None]

Context manager that controls access to event dispatcher for preventing reentrancy.

The "as" value is True if the managed code block owns the event dispatcher and False if caller higher up in the call stack already owns it. Only managed code that gets ownership (got True) is permitted to dispatch

Source code in pika/adapters/blocking_connection.py
@contextlib.contextmanager
def _acquire_event_dispatch(self) -> Generator[bool, Any, None]:
    """
    Context manager that controls access to event dispatcher for preventing reentrancy.

    The "as" value is True if the managed code block owns the event dispatcher and False if
    caller higher up in the call stack already owns it. Only managed code that gets ownership
    (got True) is permitted to dispatch
    """
    try:
        # __enter__ part
        self._event_dispatch_suspend_depth += 1
        yield self._event_dispatch_suspend_depth == 1
    finally:
        # __exit__ part
        self._event_dispatch_suspend_depth -= 1

_cleanup

_cleanup() -> None

Clean up members that might inhibit garbage collection.

Source code in pika/adapters/blocking_connection.py
def _cleanup(self) -> None:
    """Clean up members that might inhibit garbage collection."""
    with self._cleanup_mutex:
        # `getattr` rather than a plain attribute test: `__init__` may have
        # raised before `_impl` was assigned.
        if getattr(self, '_impl', None) is not None:
            self._impl.ioloop.close()
        self._ready_events.clear()
        self._closed_result.reset()

_create_connection

_create_connection(
    configs: Parameters
    | Sequence[Parameters]
    | None = None,
    impl_class: SelectConnection | None = None,
) -> SelectConnection

Run connection workflow, blocking until it completes.

PARAMETER DESCRIPTION
configs

Connection parameters instance or non-empty sequence of them. Type: None | pika.connection.Parameters | sequence.

TYPE: Parameters | Sequence[Parameters] | None DEFAULT: None

impl_class

for tests/debugging only; implementation class.

TYPE: SelectConnection | None DEFAULT: None

RAISES DESCRIPTION
Exception

on failure

Source code in pika/adapters/blocking_connection.py
def _create_connection(
    self,
    configs: pika.connection.Parameters |
    Sequence[pika.connection.Parameters] | None = None,
    impl_class: select_connection.SelectConnection | None = None
) -> select_connection.SelectConnection:
    """
    Run connection workflow, blocking until it completes.

    :param configs: Connection
        parameters instance or non-empty sequence of them.
        Type: `None | pika.connection.Parameters | sequence`.
    :param impl_class: for tests/debugging only; implementation class.


    :raises Exception: on failure
    """
    if configs is None:
        configs = (pika.connection.Parameters(),)

    if isinstance(configs, pika.connection.Parameters):
        configs = (configs,)

    if not configs:
        raise ValueError('Expected a non-empty sequence of connection '
                         f'parameters, but got {configs!r}.')

    # Connection workflow completion args
    #   `result` may be an instance of connection on success or exception on
    #   failure.
    on_cw_done_result = _CallbackResult(
        namedtuple('BlockingConnection_OnConnectionWorkflowDoneArgs',
                   'result'))

    resolved_impl_class = impl_class or select_connection.SelectConnection

    ioloop = select_connection.IOLoop()

    ioloop.activate_poller()
    try:
        resolved_impl_class.create_connection(
            configs,
            on_done=on_cw_done_result.set_value_once,
            custom_ioloop=ioloop)

        while not on_cw_done_result.ready:
            ioloop.poll()
            ioloop.process_timeouts()

        if isinstance(on_cw_done_result.value.result, BaseException):
            error = on_cw_done_result.value.result
            LOGGER.error('Connection workflow failed: %r', error)
            raise self._reap_last_connection_workflow_error(error)
        LOGGER.info('Connection workflow succeeded: %r',
                    on_cw_done_result.value.result)
        return on_cw_done_result.value.result
    except Exception:
        LOGGER.exception('Error in _create_connection().')
        ioloop.close()
        self._cleanup()
        raise

_dispatch_channel_events

_dispatch_channel_events() -> None

Invoke the _dispatch_events method on open channels that requested it.

Source code in pika/adapters/blocking_connection.py
def _dispatch_channel_events(self) -> None:
    """Invoke the `_dispatch_events` method on open channels that requested it."""
    if not self._channels_pending_dispatch:
        return

    with self._acquire_event_dispatch() as dispatch_acquired:
        if not dispatch_acquired:
            # Nested dispatch or dispatch blocked higher in call stack
            return

        candidates = list(self._channels_pending_dispatch)
        self._channels_pending_dispatch.clear()

        for channel_number in candidates:
            if channel_number < 0:
                # This was meant to terminate process_data_events
                continue

            try:
                impl_channel = self._impl._channels[channel_number]
            except KeyError:
                continue

            if impl_channel.is_open:
                impl_channel._get_cookie()._dispatch_events()

_dispatch_connection_events

_dispatch_connection_events() -> None

Dispatch ready connection events.

Source code in pika/adapters/blocking_connection.py
def _dispatch_connection_events(self) -> None:
    """Dispatch ready connection events."""
    if not self._ready_events:
        return

    with self._acquire_event_dispatch() as dispatch_acquired:
        if not dispatch_acquired:
            # Nested dispatch or dispatch blocked higher in call stack
            return

        # Limit dispatch to the number of currently ready events to avoid
        # getting stuck in this loop
        for _ in range(len(self._ready_events)):
            try:
                evt = self._ready_events.popleft()
            except IndexError:
                # Some events (e.g., timers) must have been cancelled
                break

            evt.dispatch()

_flush_output

_flush_output(*waiters: Callable[[], bool]) -> None

Flush output and process input while waiting for any of the given callbacks to return true. The wait is aborted upon connection-close. Otherwise, processing continues until the output is flushed AND at least one of the callbacks returns true. If there are no callbacks, then processing ends when all output is flushed.

PARAMETER DESCRIPTION
waiters

sequence of zero or more callables taking no args and returning true when it's time to stop processing. Their results are OR'ed together.

TYPE: Callable[[], bool] DEFAULT: ()

RAISES DESCRIPTION
Exception

exceptions passed by impl if opening of connection fails or connection closes.

Source code in pika/adapters/blocking_connection.py
def _flush_output(self, *waiters: Callable[[], bool]) -> None:
    """
    Flush output and process input while waiting for any of the given callbacks to return true.
    The wait is aborted upon connection-close. Otherwise, processing continues until the output
    is flushed AND at least one of the callbacks returns true. If there are no callbacks, then
    processing ends when all output is flushed.

    :param waiters: sequence of zero or more callables taking no args and returning true when
        it's time to stop processing. Their results are OR'ed together.
    :raises Exception: exceptions passed by impl if opening of connection fails or connection
        closes.
    """
    if self.is_closed:
        raise exceptions.ConnectionWrongStateError()

    # Conditions for terminating the processing loop:
    #   connection closed
    #         OR
    #   empty outbound buffer and no waiters
    #         OR
    #   empty outbound buffer and any waiter is ready
    def is_done() -> bool:
        return (self._closed_result.ready or
                ((not self._impl._transport or
                  self._impl._get_write_buffer_size() == 0) and
                 (not waiters or any(ready() for ready in waiters))))

    # Process I/O until our completion condition is satisfied
    while not is_done():
        self._impl.ioloop.poll()
        self._impl.ioloop.process_timeouts()

    if self._closed_result.ready:
        try:
            if not isinstance(self._closed_result.value.error,
                              exceptions.ConnectionClosedByClient):
                LOGGER.error('Unexpected connection close detected: %r',
                             self._closed_result.value.error)
                raise self._closed_result.value.error
            LOGGER.info('User-initiated close: result=%r',
                        self._closed_result.value)
        finally:
            self._cleanup()

_on_connection_blocked

_on_connection_blocked(
    user_callback: Callable[[Method[Blocked]], None],
    _impl: SelectConnection,
    method_frame: Method[Blocked],
) -> None

Handle Connection.Blocked notification from RabbitMQ broker.

PARAMETER DESCRIPTION
user_callback

callback passed to add_on_connection_blocked_callback

TYPE: Callable[[Method[Blocked]], None]

_impl

TYPE: SelectConnection

method_frame

method frame having method member of type pika.spec.Connection.Blocked

TYPE: Method[Blocked]

Source code in pika/adapters/blocking_connection.py
def _on_connection_blocked(
        self, user_callback: Callable[
            [pika.frame.Method[pika.spec.Connection.Blocked]],
            None], _impl: select_connection.SelectConnection,
        method_frame: pika.frame.Method[pika.spec.Connection.Blocked]
) -> None:
    """
    Handle Connection.Blocked notification from RabbitMQ broker.

    :param user_callback: callback passed to `add_on_connection_blocked_callback`
    :param _impl:
    :param method_frame: method frame having `method` member of type
        `pika.spec.Connection.Blocked`
    """
    self._ready_events.append(
        _ConnectionBlockedEvt(user_callback, method_frame))

_on_connection_unblocked

_on_connection_unblocked(
    user_callback: Callable[[Method[Unblocked]], None],
    _impl: SelectConnection,
    method_frame: Method[Unblocked],
) -> None

Handle Connection.Unblocked notification from RabbitMQ broker.

PARAMETER DESCRIPTION
user_callback

callback passed to add_on_connection_unblocked_callback

TYPE: Callable[[Method[Unblocked]], None]

_impl

TYPE: SelectConnection

method_frame

method frame having method member of type pika.spec.Connection.Blocked

TYPE: Method[Unblocked]

Source code in pika/adapters/blocking_connection.py
def _on_connection_unblocked(
    self, user_callback: Callable[
        [pika.frame.Method[pika.spec.Connection.Unblocked]],
        None], _impl: select_connection.SelectConnection,
    method_frame: pika.frame.Method[pika.spec.Connection.Unblocked]
) -> None:
    """
    Handle Connection.Unblocked notification from RabbitMQ broker.

    :param user_callback: callback passed to `add_on_connection_unblocked_callback`
    :param _impl:
    :param method_frame: method frame having `method` member of type
        `pika.spec.Connection.Blocked`
    """
    self._ready_events.append(
        _ConnectionUnblockedEvt(user_callback, method_frame))

_on_threadsafe_callback

_on_threadsafe_callback(
    user_callback: Callable[[], None],
) -> None

Handle callback that was registered via self._impl._adapter_add_callback_threadsafe.

PARAMETER DESCRIPTION
user_callback

callback passed to our add_callback_threadsafe by the application.

TYPE: Callable[[], None]

Source code in pika/adapters/blocking_connection.py
def _on_threadsafe_callback(self, user_callback: Callable[[],
                                                          None]) -> None:
    """Handle callback that was registered via
    `self._impl._adapter_add_callback_threadsafe`.

    :param user_callback: callback passed to our
        `add_callback_threadsafe` by the application.

    """
    # Turn it into a 0-delay timeout to take advantage of our existing logic
    # that deals with reentrancy
    self.call_later(0, user_callback)

_on_timer_ready

_on_timer_ready(evt: _TimerEvt) -> None

Handle expiry of a timer that was registered via _adapter_call_later()

PARAMETER DESCRIPTION
evt

TYPE: _TimerEvt

Source code in pika/adapters/blocking_connection.py
def _on_timer_ready(self, evt: _TimerEvt) -> None:
    """Handle expiry of a timer that was registered via
    `_adapter_call_later()`

    :param evt:

    """
    self._ready_events.append(evt)

_reap_last_connection_workflow_error staticmethod

_reap_last_connection_workflow_error(
    error: BaseException,
) -> BaseException

Extract exception value from the last connection attempt.

PARAMETER DESCRIPTION
error

error passed by the AMQPConnectionWorkflow completion callback.

TYPE: BaseException

RETURNS DESCRIPTION
BaseException

Exception value from the last connection attempt

Source code in pika/adapters/blocking_connection.py
@staticmethod
def _reap_last_connection_workflow_error(
        error: BaseException) -> BaseException:
    """
    Extract exception value from the last connection attempt.

    :param error: error passed by the `AMQPConnectionWorkflow` completion callback.
    :returns: Exception value from the last connection attempt
    """
    if isinstance(error, connection_workflow.AMQPConnectionWorkflowFailed):
        # Extract exception value from the last connection attempt
        last = error.exceptions[-1]
        if isinstance(last,
                      connection_workflow.AMQPConnectorSocketConnectError):
            error = exceptions.AMQPConnectionError(last,
                                                   host=last.host,
                                                   port=last.port)
        elif isinstance(last,
                        connection_workflow.AMQPConnectorPhaseErrorBase):
            error = last.exception
        else:
            error = last

    return error

_request_channel_dispatch

_request_channel_dispatch(channel_number: int) -> None

Called by BlockingChannel instances to request a call to their _dispatch_events method or to terminate process_data_events; BlockingConnection will honor these requests from a safe context.

PARAMETER DESCRIPTION
channel_number

positive channel number to request a call to the channel's _dispatch_events; a negative channel number to request termination of process_data_events

TYPE: int

Source code in pika/adapters/blocking_connection.py
def _request_channel_dispatch(self, channel_number: int) -> None:
    """Called by BlockingChannel instances to request a call to their
    _dispatch_events method or to terminate `process_data_events`;
    BlockingConnection will honor these requests from a safe context.

    :param channel_number: positive channel number to request a call
        to the channel's `_dispatch_events`; a negative channel number to
        request termination of `process_data_events`
    """
    self._channels_pending_dispatch.add(channel_number)

add_callback_threadsafe

add_callback_threadsafe(
    callback: Callable[..., None],
) -> None

Requests a call to the given function as soon as possible in the context of this connection's thread.

NOTE: This is the only thread-safe method in BlockingConnection. All other manipulations of BlockingConnection must be performed from the connection's thread.

NOTE: the callbacks are dispatched only in the scope of specially-designated methods: see BlockingConnection.process_data_events() and BlockingChannel.start_consuming().

For example, a thread may request a call to the BlockingChannel.basic_ack method of a BlockingConnection that is running in a different thread via::

connection.add_callback_threadsafe(
    functools.partial(channel.basic_ack, delivery_tag=...))

NOTE: if you know that the requester is running on the same thread as the connection it is more efficient to use the BlockingConnection.call_later() method with a delay of 0.

PARAMETER DESCRIPTION
callback

The callback method; must be callable

TYPE: Callable[..., None]

RAISES DESCRIPTION
pika.exceptions.ConnectionWrongStateError

if connection is closed

Source code in pika/adapters/blocking_connection.py
def add_callback_threadsafe(self, callback: Callable[..., None]) -> None:
    """
    Requests a call to the given function as soon as possible in the context of this
    connection's thread.

    NOTE: This is the only thread-safe method in `BlockingConnection`. All
    other manipulations of `BlockingConnection` must be performed from the
    connection's thread.

    NOTE: the callbacks are dispatched only in the scope of
    specially-designated methods: see
    `BlockingConnection.process_data_events()` and
    `BlockingChannel.start_consuming()`.

    For example, a thread may request a call to the
    `BlockingChannel.basic_ack` method of a `BlockingConnection` that is
    running in a different thread via::

        connection.add_callback_threadsafe(
            functools.partial(channel.basic_ack, delivery_tag=...))

    NOTE: if you know that the requester is running on the same thread as
    the connection it is more efficient to use the
    `BlockingConnection.call_later()` method with a delay of 0.

    :param callback: The callback method; must be callable
    :raises pika.exceptions.ConnectionWrongStateError: if connection is
        closed
    """
    with self._cleanup_mutex:
        # NOTE: keep in mind that we may be called from another thread and
        # this mutex only synchronizes us with our connection cleanup logic,
        # so a simple check for "is_closed" is pretty much all we're allowed
        # to do here besides calling the only thread-safe method
        # _adapter_add_callback_threadsafe().
        if self.is_closed:
            raise exceptions.ConnectionWrongStateError(
                'BlockingConnection.add_callback_threadsafe() called on '
                'closed or closing connection.')

        self._impl._adapter_add_callback_threadsafe(
            functools.partial(self._on_threadsafe_callback, callback))

add_on_connection_blocked_callback

add_on_connection_blocked_callback(
    callback: Callable[[Connection, Method[Blocked]], None],
) -> None

RabbitMQ AMQP extension - Add a callback to be notified when the connection gets blocked (Connection.Blocked received from RabbitMQ) due to the broker running low on resources (memory or disk). In this state RabbitMQ suspends processing incoming data until the connection is unblocked, so it's a good idea for publishers receiving this notification to suspend publishing until the connection becomes unblocked.

NOTE: due to the blocking nature of BlockingConnection, if it's sending outbound data while the connection is/becomes blocked, the call may remain blocked until the connection becomes unblocked, if ever. You may use ConnectionParameters.blocked_connection_timeout to abort a BlockingConnection method call with an exception when the connection remains blocked longer than the given timeout value.

See also Connection.add_on_connection_unblocked_callback()

See also ConnectionParameters.blocked_connection_timeout.

PARAMETER DESCRIPTION
callback

Callback to call on Connection.Blocked, having the signature callback(connection, pika.frame.Method), where connection is the BlockingConnection instance and the method frame's method member is of type pika.spec.Connection.Blocked

TYPE: Callable[[Connection, Method[Blocked]], None]

Source code in pika/adapters/blocking_connection.py
def add_on_connection_blocked_callback(
    self, callback: Callable[[
        pika.connection.Connection, pika.frame.Method[pika.spec.Connection.
                                                      Blocked]
    ], None]
) -> None:
    """RabbitMQ AMQP extension - Add a callback to be notified when the
    connection gets blocked (`Connection.Blocked` received from RabbitMQ)
    due to the broker running low on resources (memory or disk). In this
    state RabbitMQ suspends processing incoming data until the connection
    is unblocked, so it's a good idea for publishers receiving this
    notification to suspend publishing until the connection becomes
    unblocked.

    NOTE: due to the blocking nature of BlockingConnection, if it's sending
    outbound data while the connection is/becomes blocked, the call may
    remain blocked until the connection becomes unblocked, if ever. You
    may use `ConnectionParameters.blocked_connection_timeout` to abort a
    BlockingConnection method call with an exception when the connection
    remains blocked longer than the given timeout value.

    See also `Connection.add_on_connection_unblocked_callback()`

    See also `ConnectionParameters.blocked_connection_timeout`.

    :param callback: Callback to call on `Connection.Blocked`,
        having the signature `callback(connection, pika.frame.Method)`,
        where connection is the `BlockingConnection` instance and the method
        frame's `method` member is of type `pika.spec.Connection.Blocked`

    """
    self._impl.add_on_connection_blocked_callback(
        functools.partial(
            self._on_connection_blocked,
            functools.partial(callback,
                              cast(pika.connection.Connection, self))))

add_on_connection_unblocked_callback

add_on_connection_unblocked_callback(
    callback: Callable[
        [Connection, Method[Unblocked]], None
    ],
) -> None

RabbitMQ AMQP extension - Add a callback to be notified when the connection gets unblocked (Connection.Unblocked frame is received from RabbitMQ) letting publishers know it's ok to start publishing again.

PARAMETER DESCRIPTION
callback

Callback to call on Connection.Unblocked, having the signaturecallback(connection, pika.frame.Method), where connection is theBlockingConnectioninstance and the method frame'smethodmember is of typepika.spec.Connection.Unblocked`

TYPE: Callable[[Connection, Method[Unblocked]], None]

Source code in pika/adapters/blocking_connection.py
def add_on_connection_unblocked_callback(
    self, callback: Callable[[
        pika.connection.Connection, pika.frame.Method[pika.spec.Connection.
                                                      Unblocked]
    ], None]
) -> None:
    """RabbitMQ AMQP extension - Add a callback to be notified when the
    connection gets unblocked (`Connection.Unblocked` frame is received from
    RabbitMQ) letting publishers know it's ok to start publishing again.

    :param callback: Callback to call on Connection.Unblocked`,
        having the signature `callback(connection, pika.frame.Method)`,
        where connection is the `BlockingConnection` instance and the method
        frame's `method` member is of type `pika.spec.Connection.Unblocked`

    """
    self._impl.add_on_connection_unblocked_callback(
        functools.partial(
            self._on_connection_unblocked,
            functools.partial(callback,
                              cast(pika.connection.Connection, self))))

call_later

call_later(
    delay: float, callback: Callable[[], None]
) -> int

Create a single-shot timer to fire after delay seconds.

Do not confuse with Tornado's timeout where you pass in the time you want to have your callback called. Only pass in the seconds until it's to be called.

NOTE: the timer callbacks are dispatched only in the scope of specially-designated methods: see BlockingConnection.process_data_events() and BlockingChannel.start_consuming().

PARAMETER DESCRIPTION
delay

The number of seconds to wait to call callback

TYPE: float

callback

The callback method with the signature callback()

TYPE: Callable[[], None]

RETURNS DESCRIPTION
int

Opaque timer id

Source code in pika/adapters/blocking_connection.py
def call_later(self, delay: float, callback: Callable[[], None]) -> int:
    """
    Create a single-shot timer to fire after delay seconds.

    Do not confuse with Tornado's timeout where you pass in the time you want to
    have your callback called. Only pass in the seconds until it's to be
    called.

    NOTE: the timer callbacks are dispatched only in the scope of
    specially-designated methods: see
    `BlockingConnection.process_data_events()` and
    `BlockingChannel.start_consuming()`.

    :param delay: The number of seconds to wait to call callback
    :param callback: The callback method with the signature
        callback()
    :returns: Opaque timer id
    """
    validators.require_callback(callback)

    evt = _TimerEvt(callback=callback)
    timer_id = cast(
        int,
        self._impl._adapter_call_later(
            delay, functools.partial(self._on_timer_ready, evt)))
    evt.timer_id = timer_id

    return timer_id

channel

channel(
    channel_number: int | None = None,
) -> BlockingChannel

Create a new channel with the next available channel number or pass in a channel number to use. Must be non-zero if you would like to specify but it is recommended that you let Pika manage the channel numbers.

PARAMETER DESCRIPTION
channel_number

optional channel number to use

TYPE: int | None DEFAULT: None

Source code in pika/adapters/blocking_connection.py
def channel(self, channel_number: int | None = None) -> BlockingChannel:
    """
    Create a new channel with the next available channel number or pass in a channel number to
    use. Must be non-zero if you would like to specify but it is recommended that you let Pika
    manage the channel numbers.

    :param channel_number: optional channel number to use
    """
    with _CallbackResult(self._OnChannelOpenedArgs) as opened_args:
        impl_channel = self._impl.channel(
            channel_number=channel_number,
            on_open_callback=opened_args.set_value_once)

        # Create our proxy channel
        channel = BlockingChannel(impl_channel, self)

        # Link implementation channel with our proxy channel
        impl_channel._set_cookie(channel)

        # Drive I/O until Channel.Open-ok
        channel._flush_output(opened_args.is_ready)

    return channel

close

close(
    reply_code: int = 200,
    reply_text: str = 'Normal shutdown',
) -> None

Disconnect from RabbitMQ.

If there are any open channels, it will attempt to close them prior to fully disconnecting. Channels which have active consumers will attempt to send a Basic.Cancel to RabbitMQ to cleanly stop the delivery of messages prior to closing the channel.

PARAMETER DESCRIPTION
reply_code

The code number for the close

TYPE: int DEFAULT: 200

reply_text

The text reason for the close

TYPE: str DEFAULT: 'Normal shutdown'

RAISES DESCRIPTION
pika.exceptions.ConnectionWrongStateError

if called on a closed connection (NEW in v1.0.0)

Source code in pika/adapters/blocking_connection.py
def close(self,
          reply_code: int = 200,
          reply_text: str = 'Normal shutdown') -> None:
    """
    Disconnect from RabbitMQ.

    If there are any open channels, it will attempt to close them prior to fully disconnecting.
    Channels which have active consumers will attempt to send a Basic.Cancel to RabbitMQ to
    cleanly stop the delivery of messages prior to closing the channel.

    :param reply_code: The code number for the close
    :param reply_text: The text reason for the close
    :raises pika.exceptions.ConnectionWrongStateError: if called on a closed connection (NEW in
        v1.0.0)
    """
    if not self.is_open:
        msg = f'{self.__class__.__name__}.close({reply_code}, {reply_text!r}) called on closed connection.'
        LOGGER.error(msg)
        raise exceptions.ConnectionWrongStateError(msg)

    LOGGER.info('Closing connection (%s): %s', reply_code, reply_text)

    # Close channels that remain opened
    for impl_channel in list(self._impl._channels.values()):
        channel = impl_channel._get_cookie()
        if channel.is_open:
            try:
                channel.close(reply_code, reply_text)
            except exceptions.ChannelClosed as exc:
                # Log and suppress broker-closed channel
                LOGGER.warning(
                    'Got ChannelClosed while closing channel '
                    'from connection.close: %r', exc)

    # Close the connection
    self._impl.close(reply_code, reply_text)

    self._flush_output(self._closed_result.is_ready)

process_data_events

process_data_events(time_limit: float | None = 0) -> None

Will make sure that data events are processed.

Dispatches timer and channel callbacks if not called from the scope of BlockingConnection or BlockingChannel callback. Your app can block on this method. If your application maintains a long-lived publisher connection, this method should be called periodically in order to respond to heartbeats and other data events. See examples/long_running_publisher.py for an example.

PARAMETER DESCRIPTION
time_limit

suggested upper bound on processing time in seconds. The actual blocking time depends on the granularity of the underlying ioloop. Zero means return as soon as possible. None means there is no limit on processing time and the function will block until I/O produces actionable events. Defaults to 0 for backward compatibility. This parameter is NEW in pika 0.10.0.

TYPE: float | None DEFAULT: 0

Source code in pika/adapters/blocking_connection.py
def process_data_events(self, time_limit: float | None = 0) -> None:
    """
    Will make sure that data events are processed.

    Dispatches timer and channel callbacks if not called from the scope of BlockingConnection or
    BlockingChannel callback. Your app can block on this method. If your application maintains a
    long-lived publisher connection, this method should be called periodically in order to
    respond to heartbeats and other data events. See `examples/long_running_publisher.py` for an
    example.

    :param time_limit: suggested upper bound on processing time in seconds. The actual blocking
        time depends on the granularity of the underlying ioloop. Zero means return as soon as
        possible. None means there is no limit on processing time and the function will block
        until I/O produces actionable events. Defaults to 0 for backward compatibility. This
        parameter is NEW in pika 0.10.0.
    """
    with self._acquire_event_dispatch() as dispatch_acquired:
        # Check if we can actually process pending events
        def common_terminator() -> bool:
            return bool(
                dispatch_acquired and
                (self._channels_pending_dispatch or self._ready_events))

        if time_limit is None:
            self._flush_output(common_terminator)
        else:
            with _IoloopTimerContext(time_limit, self._impl) as timer:
                self._flush_output(timer.is_ready, common_terminator)

    if self._ready_events:
        self._dispatch_connection_events()

    if self._channels_pending_dispatch:
        self._dispatch_channel_events()

    if self._server_channel_closures:
        exc = self._server_channel_closures.popleft()
        LOGGER.debug('Channel closed by broker: %r', exc)
        raise exc

remove_timeout

remove_timeout(timeout_id: int) -> None

Remove a timer if it's still in the timeout stack.

PARAMETER DESCRIPTION
timeout_id

The opaque timer id to remove

TYPE: int

Source code in pika/adapters/blocking_connection.py
def remove_timeout(self, timeout_id: int) -> None:
    """
    Remove a timer if it's still in the timeout stack.

    :param timeout_id: The opaque timer id to remove
    """
    # Remove from the impl's timeout stack
    self._impl._adapter_remove_timeout(timeout_id)

    # Remove from ready events, if the timer fired already
    for i, evt in enumerate(self._ready_events):
        if isinstance(evt, _TimerEvt) and evt.timer_id == timeout_id:
            index_to_remove = i
            break
    else:
        # Not found
        return

    del self._ready_events[index_to_remove]

sleep

sleep(duration: float) -> None

A safer way to sleep than calling time.sleep() directly that would keep the adapter from ignoring frames sent from the broker. The connection will "sleep" or block the number of seconds specified in duration in small intervals.

PARAMETER DESCRIPTION
duration

The time to sleep in seconds

TYPE: float

Source code in pika/adapters/blocking_connection.py
def sleep(self, duration: float) -> None:
    """
    A safer way to sleep than calling time.sleep() directly that would keep the adapter from
    ignoring frames sent from the broker. The connection will "sleep" or block the number of
    seconds specified in duration in small intervals.

    :param duration: The time to sleep in seconds
    """
    assert duration >= 0, duration

    deadline = pika._utils.time_now() + duration
    time_limit = duration
    # Process events at least once
    while True:
        self.process_data_events(time_limit)
        time_limit = deadline - pika._utils.time_now()
        if time_limit <= 0:
            break

update_secret

update_secret(new_secret: str, reason: str) -> None

RabbitMQ AMQP extension - This method updates the secret used to authenticate this connection. It is used when secrets have an expiration date and need to be renewed, like OAuth 2 tokens.

PARAMETER DESCRIPTION
new_secret

The new secret

TYPE: str

reason

The reason for the secret update

TYPE: str

RAISES DESCRIPTION
pika.exceptions.ConnectionWrongStateError

if connection is not open.

Source code in pika/adapters/blocking_connection.py
def update_secret(self, new_secret: str, reason: str) -> None:
    """RabbitMQ AMQP extension - This method updates the secret used to authenticate this connection.
    It is used when secrets have an expiration date and need to be renewed, like OAuth 2 tokens.

    :param new_secret: The new secret
    :param reason: The reason for the secret update

    :raises pika.exceptions.ConnectionWrongStateError: if connection is
        not open.
    """
    result = _CallbackResult()
    self._impl.update_secret(new_secret, reason, result.signal_once)
    self._flush_output(result.is_ready)

BlockingChannel

The BlockingChannel implements blocking semantics for most things that one would use callback-passing-style for with the :py:class:~pika.channel.Channel class. In addition, the BlockingChannel class implements a :term:generator that allows you to :doc:consume messages </examples/blocking_consumer_generator> without using callbacks.

Example of creating a BlockingChannel::

import pika

# Create our connection object
connection = pika.BlockingConnection()

# The returned object will be a synchronous channel
channel = connection.channel()
Source code in pika/adapters/blocking_connection.py
class BlockingChannel:
    """The BlockingChannel implements blocking semantics for most things that
    one would use callback-passing-style for with the
    :py:class:`~pika.channel.Channel` class. In addition,
    the `BlockingChannel` class implements a :term:`generator` that allows
    you to :doc:`consume messages </examples/blocking_consumer_generator>`
    without using callbacks.

    Example of creating a BlockingChannel::

        import pika

        # Create our connection object
        connection = pika.BlockingConnection()

        # The returned object will be a synchronous channel
        channel = connection.channel()

    """

    class _RxMessageArgs(NamedTuple):
        channel: Any
        method: Any
        properties: Any
        body: bytes

    class _MethodFrameCallbackResultArgs(NamedTuple):
        method_frame: Any

    class _OnMessageConfirmationReportArgs(NamedTuple):
        method_frame: Any

    class _FlowOkCallbackResultArgs(NamedTuple):
        active: bool

    _CONSUMER_CANCELLED_CB_KEY = 'blocking_channel_consumer_cancelled'

    def __init__(self, channel_impl: pika.channel.Channel,
                 connection: BlockingConnection) -> None:
        """
        Create a new instance of the Channel.

        :param channel_impl: Channel implementation object as returned from
            SelectConnection.channel()
        :param connection: The connection object
        """
        self._impl = channel_impl
        self._connection = connection

        # A mapping of consumer tags to _ConsumerInfo for active consumers
        self._consumer_infos: dict[Any, Any] = {}

        # Queue consumer generator generator info of type
        # _QueueConsumerGeneratorInfo created by BlockingChannel.consume
        self._queue_consumer_generator: _QueueConsumerGeneratorInfo | None = None

        # Whether RabbitMQ delivery confirmation has been enabled
        self._delivery_confirmation = False

        # Receives message delivery confirmation report (Basic.ack or
        # Basic.nack) from broker when delivery confirmations are enabled
        self._message_confirmation_result = _CallbackResult(
            self._OnMessageConfirmationReportArgs)

        # deque of pending events: _ConsumerDeliveryEvt and
        # _ConsumerCancellationEvt objects that will be returned by
        # `BlockingChannel.get_event()`
        self._pending_events: deque[Any] = deque()

        # Holds a ReturnedMessage object representing a message received via
        # Basic.Return in publisher-acknowledgments mode.
        self._puback_return: ReturnedMessage | None = None

        # self._on_channel_closed() saves the reason exception here
        self._closing_reason = None  # type: None | Exception

        # Receives Basic.ConsumeOk reply from server
        self._basic_consume_ok_result = _CallbackResult()

        # Receives args from Basic.GetEmpty response
        #  https://www.rabbitmq.com/amqp-0-9-1-reference.html#basic.get
        self._basic_getempty_result = _CallbackResult(
            self._MethodFrameCallbackResultArgs)

        self._impl.add_on_cancel_callback(self._on_consumer_cancelled_by_broker)

        self._impl.add_callback(self._basic_consume_ok_result.signal_once,
                                replies=[pika.spec.Basic.ConsumeOk],
                                one_shot=False)

        self._impl.add_on_close_callback(self._on_channel_closed)

        self._impl.add_callback(self._basic_getempty_result.set_value_once,
                                replies=[pika.spec.Basic.GetEmpty],
                                one_shot=False)

        LOGGER.info('Created channel=%s', self.channel_number)

    def __int__(self) -> int:
        """
        Return the channel object as its channel number.

        NOTE: inherited from legacy BlockingConnection; might be error-prone;
        use `channel_number` property instead.
        """
        return self.channel_number

    @override
    def __repr__(self) -> str:
        return f'<{self.__class__.__name__} impl={self._impl!r}>'

    def __enter__(self) -> Self:
        return self

    def __exit__(self, exc_type: type[BaseException] | None,
                 value: BaseException | None,
                 traceback: TracebackType | None) -> None:
        if self.is_open:
            self.close()

    def _cleanup(self) -> None:
        """Clean up members that might inhibit garbage collection."""
        self._message_confirmation_result.reset()
        self._pending_events = deque()
        self._consumer_infos = {}
        self._queue_consumer_generator = None

    @property
    def channel_number(self) -> int:
        """Channel number."""
        return self._impl.channel_number

    @property
    def connection(self) -> BlockingConnection:
        """The channel's BlockingConnection instance."""
        return self._connection

    @property
    def is_closed(self) -> bool:
        """Returns True if the channel is closed."""
        return self._impl.is_closed

    @property
    def is_open(self) -> bool:
        """Returns True if the channel is open."""
        return self._impl.is_open

    @property
    def consumer_tags(self) -> list[str]:
        """Property method that returns a list of consumer tags for active consumers."""
        return list(self._consumer_infos.keys())

    _ALWAYS_READY_WAITERS = ((lambda: True),)

    def _flush_output(self, *waiters: Callable[[], bool]) -> None:
        """
        Flush output and process input while waiting for any of the given callbacks to return true.
        The wait is aborted upon channel-close or connection-close. Otherwise, processing continues
        until the output is flushed AND at least one of the callbacks returns true. If there are no
        callbacks, then processing ends when all output is flushed.

        :param waiters: sequence of zero or more callables taking no args and returning true when
            it's time to stop processing. Their results are OR'ed together. An empty sequence is
            treated as equivalent to a waiter always returning true.
        """
        if self.is_closed:
            self._impl._raise_if_not_open()

        if not waiters:
            waiters = self._ALWAYS_READY_WAITERS

        self._connection._flush_output(lambda: self.is_closed, *waiters)

        if self.is_closed and isinstance(self._closing_reason,
                                         exceptions.ChannelClosedByBroker):
            raise self._closing_reason

    def _on_puback_message_returned(self, channel: pika.channel.Channel,
                                    method: pika.spec.Basic.Return,
                                    properties: pika.spec.BasicProperties,
                                    body: bytes) -> None:
        """
        Called as the result of Basic.Return from broker in publisher-acknowledgements mode.

        Saves the info as a ReturnedMessage instance in self._puback_return.

        :param channel: our self._impl channel
        :param method:
        :param properties: message properties
        :param body: returned message body; empty string if no body
        """
        assert channel is self._impl, (channel.channel_number,
                                       self.channel_number)

        assert isinstance(method, pika.spec.Basic.Return), method
        assert isinstance(properties, pika.spec.BasicProperties), (properties)

        LOGGER.warning(
            'Published message was returned: _delivery_confirmation=%s; '
            'channel=%s; method=%r; properties=%r; body_size=%d; '
            'body_prefix=%.255r', self._delivery_confirmation,
            channel.channel_number, method, properties,
            len(body) if body is not None else None, body)

        self._puback_return = ReturnedMessage(method, properties, body)

    def _add_pending_event(self, evt: _ChannelPendingEvt) -> None:
        """
        Append an event to the channel's list of events that are ready for dispatch to user and
        signal our connection that this channel is ready for event dispatch.

        :param evt: an event derived from _ChannelPendingEvt
        """
        self._pending_events.append(evt)
        self.connection._request_channel_dispatch(self.channel_number)

    def _on_channel_closed(self, _channel: pika.channel.Channel,
                           reason: Exception) -> None:
        """
        Callback from impl notifying us that the channel has been closed.

        This may be as the result of user-, broker-, or internal connection clean-up initiated closing or meta-closing of the
        channel.

        If it resulted from receiving `Channel.Close` from broker, we will
        expedite waking up of the event subsystem so that it may respond by
        raising `ChannelClosed` from user's context.

        NOTE: We can't raise exceptions in callbacks in order to protect
        the integrity of the underlying implementation. BlockingConnection's
        underlying asynchronous connection adapter (SelectConnection) uses
        callbacks to communicate with us. If BlockingConnection leaks exceptions
        back into the I/O loop or the asynchronous connection adapter, we
        interrupt their normal workflow and introduce a high likelihood of state
        inconsistency.

        See `pika.Channel.add_on_close_callback()` for additional documentation.

        :param _channel: (unused)
        :param reason:
        """
        LOGGER.debug('_on_channel_closed: %r; %r', reason, self)

        self._closing_reason = reason

        if isinstance(reason, exceptions.ChannelClosedByBroker):
            self.connection._server_channel_closures.append(reason)
            self._cleanup()

            # Request urgent termination of `process_data_events()`, in case
            # it's executing or next time it will execute
            self.connection._request_channel_dispatch(-self.channel_number)

    def _on_consumer_cancelled_by_broker(
            self,
            method_frame: pika.frame.Method[pika.spec.Basic.Cancel]) -> None:
        """
        Called by impl when broker cancels consumer via Basic.Cancel.

        This is a RabbitMQ-specific feature. The circumstances include deletion of queue being
        consumed as well as failure of a HA node responsible for the queue being consumed.

        :param method_frame: method frame with the `spec.Basic.Cancel` method
        """
        evt = _ConsumerCancellationEvt(method_frame)

        consumer = self._consumer_infos[method_frame.method.consumer_tag]

        # Don't interfere with client-initiated cancellation flow
        if not consumer.tearing_down:
            consumer.state = _ConsumerInfo.CANCELLED_BY_BROKER

        if consumer.alternate_event_sink is not None:
            consumer.alternate_event_sink(evt)
        else:
            self._add_pending_event(evt)

    def _on_consumer_message_delivery(self, _channel: pika.channel.Channel,
                                      method: pika.spec.Basic.Deliver,
                                      properties: pika.spec.BasicProperties,
                                      body: bytes) -> None:
        """
        Called by impl when a message is delivered for a consumer.

        :param _channel: The implementation channel object
        :param method:
        :param properties: message properties
        :param body: delivered message body; empty string if no body
        """
        evt = _ConsumerDeliveryEvt(method, properties, body)

        consumer = self._consumer_infos[method.consumer_tag]

        if consumer.alternate_event_sink is not None:
            consumer.alternate_event_sink(evt)
        else:
            self._add_pending_event(evt)

    def _on_consumer_generator_event(self, evt: _ChannelPendingEvt) -> None:
        """
        Sink for the queue consumer generator's consumer events; append the event to queue consumer
        generator's pending events buffer.

        :param evt: an object of type _ConsumerDeliveryEvt or _ConsumerCancellationEvt
        """
        assert self._queue_consumer_generator is not None
        self._queue_consumer_generator.pending_events.append(evt)
        # Schedule termination of connection.process_data_events using a
        # negative channel number
        self.connection._request_channel_dispatch(-self.channel_number)

    def _cancel_all_consumers(self) -> None:
        """
        Cancel all consumers.

        NOTE: pending non-ackable messages will be lost; pending ackable
        messages will be rejected.
        """
        if self._consumer_infos:
            LOGGER.debug('Cancelling %i consumers', len(self._consumer_infos))

            if self._queue_consumer_generator is not None:
                # Cancel queue consumer generator
                self.cancel()

            # Cancel consumers created via basic_consume
            for consumer_tag in list(self._consumer_infos.keys()):
                self.basic_cancel(consumer_tag)

    def _dispatch_events(self) -> None:
        """
        Called by BlockingConnection to dispatch pending events.

        `BlockingChannel` schedules this callback via `BlockingConnection._request_channel_dispatch`
        """
        while self._pending_events:
            evt = self._pending_events.popleft()

            if type(evt) is _ConsumerDeliveryEvt:
                consumer_info = self._consumer_infos[evt.method.consumer_tag]
                consumer_info.on_message_callback(self, evt.method,
                                                  evt.properties, evt.body)

            elif type(evt) is _ConsumerCancellationEvt:
                del self._consumer_infos[evt.method_frame.method.consumer_tag]

                self._impl.callbacks.process(self.channel_number,
                                             self._CONSUMER_CANCELLED_CB_KEY,
                                             self, evt.method_frame)
            else:
                evt.dispatch()

    def close(self,
              reply_code: int = 0,
              reply_text: str = 'Normal shutdown') -> None:
        """
        Will invoke a clean shutdown of the channel with the AMQP Broker.

        :param reply_code: The reply code to close the channel with
        :param reply_text: The reply text to close the channel with
        """
        LOGGER.debug('Channel.close(%s, %s)', reply_code, reply_text)

        self._impl._raise_if_not_open()

        try:
            # Cancel remaining consumers
            self._cancel_all_consumers()

            # Close the channel
            self._impl.close(reply_code=reply_code, reply_text=reply_text)
            self._flush_output(lambda: self.is_closed)
        finally:
            self._cleanup()

    def flow(self, active: bool) -> bool:
        """
        Turn Channel flow control off and on.

        NOTE: RabbitMQ doesn't support active=False; per
        https://www.rabbitmq.com/specification.html: "active=false is not
        supported by the server. Limiting prefetch with basic.qos provides much
        better control"

        For more information, please reference:

        https://www.rabbitmq.com/amqp-0-9-1-reference.html#channel.flow

        :param active: Turn flow on (True) or off (False)
        :returns: True if broker will start or continue sending; False if not
        """
        with _CallbackResult(self._FlowOkCallbackResultArgs) as flow_ok_result:
            self._impl.flow(active=active,
                            callback=flow_ok_result.set_value_once)
            self._flush_output(flow_ok_result.is_ready)
            return flow_ok_result.value.active

    def add_on_cancel_callback(
        self, callback: Callable[[pika.frame.Method[pika.spec.Basic.Cancel]],
                                 None]
    ) -> None:
        """
        Pass a callback function that will be called when Basic.Cancel is sent by the broker.

        The callback function should receive a method frame parameter.

        :param callback: a callable for handling broker's Basic.Cancel
            notification with the call signature: callback(method_frame)
            where method_frame is of type `pika.frame.Method` with method of
            type `spec.Basic.Cancel`
        """
        self._impl.callbacks.add(self.channel_number,
                                 self._CONSUMER_CANCELLED_CB_KEY,
                                 callback,
                                 one_shot=False)

    def add_on_return_callback(
        self, callback: Callable[[
            BlockingChannel, pika.spec.Basic.Return, pika.spec.
            BasicProperties, bytes
        ], None]
    ) -> None:
        """
        Pass a callback function that will be called when a published message is rejected and
        returned by the server via `Basic.Return`.

        :param callback: The method to call on callback with the
            signature callback(channel, method, properties, body), where
            - channel: pika.Channel
            - method: pika.spec.Basic.Return
            - properties: pika.spec.BasicProperties
            - body: bytes
        """
        self._impl.add_on_return_callback(
            lambda _channel, method, properties, body: (self._add_pending_event(
                _ReturnedMessageEvt(callback, self, method, properties, body))))

    def basic_consume(self,
                      queue: str,
                      on_message_callback: Callable[[
                          BlockingChannel, pika.spec.Basic.Deliver, pika.spec.
                          BasicProperties, bytes
                      ], None],
                      auto_ack: bool = False,
                      exclusive: bool = False,
                      consumer_tag: str | None = None,
                      arguments: dict[str, Any] | None = None) -> str:
        """
        Sends the AMQP command Basic.Consume to the broker and binds messages for the consumer_tag
        to the consumer callback. If you do not pass in a consumer_tag, one will be automatically
        generated for you. Returns the consumer tag.

        NOTE: the consumer callbacks are dispatched only in the scope of
        specially-designated methods: see
        `BlockingConnection.process_data_events` and
        `BlockingChannel.start_consuming`.

        For more information about Basic.Consume, see:
        https://www.rabbitmq.com/amqp-0-9-1-reference.html#basic.consume

        :param queue: The queue from which to consume
        :param on_message_callback: Required function for dispatching messages
            to user, having the signature:
            on_message_callback(channel, method, properties, body)
            - channel: BlockingChannel
            - method: spec.Basic.Deliver
            - properties: spec.BasicProperties
            - body: bytes
        :param auto_ack: if set to True, automatic acknowledgement mode will be used
                              (see https://www.rabbitmq.com/confirms.html). This corresponds
                              with the 'no_ack' parameter in the basic.consume AMQP 0.9.1
                              method
        :param exclusive: Don't allow other consumers on the queue
        :param consumer_tag: You may specify your own consumer tag; if left
          empty, a consumer tag will be generated automatically
        :param arguments: Custom key/value pair arguments for the consumer
        :returns: consumer tag
        :raises pika.exceptions.DuplicateConsumerTag: if consumer with given
            consumer_tag is already present.
        """
        validators.require_string(queue, 'queue')
        validators.require_callback(on_message_callback, 'on_message_callback')
        return self._basic_consume_impl(queue=queue,
                                        on_message_callback=on_message_callback,
                                        auto_ack=auto_ack,
                                        exclusive=exclusive,
                                        consumer_tag=consumer_tag,
                                        arguments=arguments)

    def _basic_consume_impl(
        self,
        queue: str,
        auto_ack: bool,
        exclusive: bool,
        consumer_tag: str | None,
        arguments: dict[str, Any] | None = None,
        on_message_callback: Callable[[
            BlockingChannel, pika.spec.Basic.Deliver, pika.spec.
            BasicProperties, bytes
        ], None] | None = None,
        alternate_event_sink: Callable[[_ChannelPendingEvt], None] | None = None
    ) -> str:
        """
        The low-level implementation used by `basic_consume` and `consume`.

        See `basic_consume` docstring for more info.

        NOTE: exactly one of on_message_callback/alternate_event_sink musts be
        non-None.

        This method has one additional parameter alternate_event_sink over the
        args described in `basic_consume`.

        :param queue: Queue name
        :param auto_ack: If True, the broker acknowledges messages automatically
        :param exclusive: If True, restrict access to the current connection
        :param consumer_tag: if specified, only the consumer with the given tag will be cancelled
        :param arguments: Custom key/value pair arguments for the consumer
        :param on_message_callback: Callback for delivered messages
        :param alternate_event_sink: if specified, _ConsumerDeliveryEvt
            and _ConsumerCancellationEvt objects will be diverted to this
            callback instead of being deposited in the channel's
            `_pending_events` container. Signature:
            alternate_event_sink(evt)

        :raises pika.exceptions.DuplicateConsumerTag: if consumer with given
            consumer_tag is already present.
        """
        if (on_message_callback is None) == (alternate_event_sink is None):
            raise ValueError(((
                'exactly one of on_message_callback/alternate_event_sink must '
                'be non-None'), on_message_callback, alternate_event_sink))

        if not consumer_tag:
            # Need a consumer tag to register consumer info before sending
            # request to broker, because I/O might dispatch incoming messages
            # immediately following Basic.Consume-ok before _flush_output
            # returns
            consumer_tag = self._impl._generate_consumer_tag()

        if consumer_tag in self._consumer_infos:
            raise exceptions.DuplicateConsumerTag(consumer_tag)

        # Create new consumer
        self._consumer_infos[consumer_tag] = _ConsumerInfo(
            consumer_tag,
            auto_ack=auto_ack,
            on_message_callback=on_message_callback,
            alternate_event_sink=alternate_event_sink)

        try:
            with self._basic_consume_ok_result as ok_result:
                tag = self._impl.basic_consume(
                    on_message_callback=self._on_consumer_message_delivery,
                    queue=queue,
                    auto_ack=auto_ack,
                    exclusive=exclusive,
                    consumer_tag=consumer_tag,
                    arguments=arguments)

                assert tag == consumer_tag, (tag, consumer_tag)

                self._flush_output(ok_result.is_ready)
        except Exception:
            # If channel was closed, self._consumer_infos will be empty
            if consumer_tag in self._consumer_infos:
                del self._consumer_infos[consumer_tag]
                # Schedule termination of connection.process_data_events using a
                # negative channel number
                self.connection._request_channel_dispatch(-self.channel_number)
            raise

        # NOTE: Consumer could get cancelled by broker immediately after opening
        # (e.g., queue getting deleted externally)
        if self._consumer_infos[consumer_tag].setting_up:
            self._consumer_infos[consumer_tag].state = _ConsumerInfo.ACTIVE

        return consumer_tag

    def basic_cancel(
        self, consumer_tag: str
    ) -> Sequence[tuple[pika.spec.Basic.Deliver, pika.spec.BasicProperties,
                        bytes]]:
        """
        This method cancels a consumer.

        This does not affect already delivered messages, but it does mean the server will not send any more
        messages for that consumer. The client may receive an arbitrary number
        of messages in between sending the cancel method and receiving the
        cancel-ok reply.

        NOTE: When cancelling an auto_ack=False consumer, this implementation
        automatically Nacks and suppresses any incoming messages that have not
        yet been dispatched to the consumer's callback. However, when cancelling
        a auto_ack=True consumer, this method will return any pending messages
        that arrived before broker confirmed the cancellation.

        :param consumer_tag: Identifier for the consumer; the result of
            passing a consumer_tag that was created on another channel is
            undefined (bad things will happen)
        :returns: (NEW IN pika 0.10.0) empty sequence for a auto_ack=False
            consumer; for a auto_ack=True consumer, returns a (possibly empty)
            sequence of pending messages that arrived before broker confirmed
            the cancellation (this is done instead of via consumer's callback in
            order to prevent reentrancy/recursion. Each message is three-tuple:
            (method, properties, body)
            - method: spec.Basic.Deliver
            - properties: spec.BasicProperties
            - body: bytes
        """
        try:
            consumer_info = self._consumer_infos[consumer_tag]
        except KeyError:
            LOGGER.warning(
                'User is attempting to cancel an unknown consumer=%s; '
                'already cancelled by user or broker?', consumer_tag)
            return []

        try:
            # Assertion failure here is most likely due to reentrance
            assert consumer_info.active or consumer_info.cancelled_by_broker, (
                consumer_info.state)

            # Assertion failure here signals disconnect between consumer state
            # in BlockingChannel and Channel
            assert (consumer_info.cancelled_by_broker or
                    consumer_tag in self._impl._consumers), consumer_tag

            auto_ack = consumer_info.auto_ack

            consumer_info.state = _ConsumerInfo.TEARING_DOWN

            with _CallbackResult() as cancel_ok_result:
                # Nack pending messages for auto_ack=False consumer
                if not auto_ack:
                    pending_messages = self._remove_pending_deliveries(
                        consumer_tag)
                    if pending_messages:
                        # NOTE: we use impl's basic_reject to avoid the
                        # possibility of redelivery before basic_cancel takes
                        # control of nacking.
                        # NOTE: we can't use basic_nack with the multiple option
                        # to avoid nacking messages already held by our client.
                        for message in pending_messages:
                            delivery_tag = message.method.delivery_tag
                            # The generated spec types delivery_tag as
                            # optional, but the broker always sets it on a
                            # delivery.
                            assert delivery_tag is not None
                            self._impl.basic_reject(delivery_tag, requeue=True)

                # Cancel the consumer; impl takes care of rejecting any
                # additional deliveries that arrive for a auto_ack=False
                # consumer
                self._impl.basic_cancel(consumer_tag=consumer_tag,
                                        callback=cancel_ok_result.signal_once)

                # Flush output and wait for Basic.Cancel-ok or
                # broker-initiated Basic.Cancel
                self._flush_output(
                    cancel_ok_result.is_ready,
                    lambda: consumer_tag not in self._impl._consumers)

            if auto_ack:
                # Return pending messages for auto_ack=True consumer
                return [(evt.method, evt.properties, evt.body)
                        for evt in self._remove_pending_deliveries(consumer_tag)
                       ]
            # impl takes care of rejecting any incoming deliveries during
            # cancellation
            messages = self._remove_pending_deliveries(consumer_tag)
            assert not messages, messages

            return []
        finally:
            # NOTE: The entry could be purged if channel or connection closes
            if consumer_tag in self._consumer_infos:
                del self._consumer_infos[consumer_tag]
                # Schedule termination of connection.process_data_events using a
                # negative channel number
                self.connection._request_channel_dispatch(-self.channel_number)

    def _remove_pending_deliveries(
            self, consumer_tag: str) -> Sequence[_ConsumerDeliveryEvt]:
        """
        Extract _ConsumerDeliveryEvt objects destined for the given consumer from pending events,
        discarding the _ConsumerCancellationEvt, if any.

        :param consumer_tag:

        :returns: a (possibly empty) sequence of _ConsumerDeliveryEvt destined for the given
            consumer tag
        """
        remaining_events: deque[Any] = deque()
        unprocessed_messages: list[Any] = []
        while self._pending_events:
            evt = self._pending_events.popleft()
            if (type(evt) is _ConsumerDeliveryEvt and
                    evt.method.consumer_tag == consumer_tag):
                unprocessed_messages.append(evt)
                continue
            if (type(evt) is _ConsumerCancellationEvt and
                    evt.method_frame.method.consumer_tag == consumer_tag):
                # A broker-initiated Basic.Cancel must have arrived
                # before our cancel request completed
                continue

            remaining_events.append(evt)

        self._pending_events = remaining_events

        return unprocessed_messages

    def start_consuming(self) -> None:
        """
        Processes I/O events and dispatches timers and `basic_consume` callbacks until all consumers
        are cancelled.

        NOTE: this blocking function may not be called from the scope of a
        pika callback, because dispatching `basic_consume` callbacks from this
        context would constitute recursion.

        :raises pika.exceptions.ReentrancyError: if called from the scope of a
            `BlockingConnection` or `BlockingChannel` callback
        :raises ChannelClosed: when this channel is closed by broker.
        """
        # Check if called from the scope of an event dispatch callback
        with self.connection._acquire_event_dispatch() as dispatch_allowed:
            if not dispatch_allowed:
                raise exceptions.ReentrancyError(
                    'start_consuming may not be called from the scope of '
                    'another BlockingConnection or BlockingChannel callback')

        self._impl._raise_if_not_open()

        # Process events as long as consumers exist on this channel
        while self._consumer_infos:
            # This will raise ChannelClosed if channel is closed by broker
            self._process_data_events(time_limit=None)

    def stop_consuming(self, consumer_tag: str | None = None) -> None:
        """
        Cancels all consumers, signalling the `start_consuming` loop to exit.

        NOTE: pending non-ackable messages will be lost; pending ackable
        messages will be rejected.

        :param consumer_tag: if specified, only the consumer with the given tag will be cancelled
        """
        if consumer_tag:
            self.basic_cancel(consumer_tag)
        else:
            self._cancel_all_consumers()

    def consume(
        self,
        queue: str,
        auto_ack: bool = False,
        exclusive: bool = False,
        arguments: dict[str, Any] | None = None,
        inactivity_timeout: float | None = None,
        consumer_tag: str | None = None
    ) -> Generator[tuple[pika.spec.Basic.Deliver | None,
                         pika.spec.BasicProperties | None, bytes | None]]:
        """
        Blocking consumption of a queue instead of via a callback.

        This method is a generator that yields each message as a tuple of method,
        properties, and body. The active generator iterator terminates when the
        consumer is cancelled by client via `BlockingChannel.cancel()` or by
        broker.

        Example:
        ::
            for method, properties, body in channel.consume('queue'):
                print(body)
                channel.basic_ack(method.delivery_tag)

        You should call `BlockingChannel.cancel()` when you escape out of the
        generator loop.

        If you don't cancel this consumer, then next call on the same channel
        to `consume()` with the exact same (queue, auto_ack, exclusive) parameters
        will resume the existing consumer generator; however, calling with
        different parameters will result in an exception.

        :param queue: The queue name to consume
        :param auto_ack: Tell the broker to not expect a ack/nack response
        :param exclusive: Don't allow other consumers on the queue
        :param arguments: Custom key/value pair arguments for the consumer
        :param inactivity_timeout: if a number is given (in
            seconds), will cause the method to yield (None, None, None) after the
            given period of inactivity; this permits for pseudo-regular maintenance
            activities to be carried out by the user while waiting for messages
            to arrive. If None is given (default), then the method blocks until
            the next event arrives. NOTE that timing granularity is limited by
            the timer resolution of the underlying implementation.
            NEW in pika 0.10.0.
        :param consumer_tag: Specify your own consumer tag

        :yields: tuple(spec.Basic.Deliver, spec.BasicProperties, bytes)

        :raises ValueError: if consumer-creation parameters don't match those
            of the existing queue consumer generator, if any.
            NEW in pika 0.10.0
        :raises ChannelClosed: when this channel is closed by broker.
        """
        self._impl._raise_if_not_open()

        params = (queue, auto_ack, exclusive)

        if self._queue_consumer_generator is not None:
            if params != self._queue_consumer_generator.params:
                raise ValueError(
                    'Consume with different params not allowed on existing '
                    f'queue consumer generator; previous params: {self._queue_consumer_generator.params!r}; '
                    f'new params: {(queue, auto_ack, exclusive)!r}')
        else:
            LOGGER.debug('Creating new queue consumer generator; params: %r',
                         params)
            # Need a consumer tag to register consumer info before sending
            # request to broker, because I/O might pick up incoming messages
            # in addition to Basic.Consume-ok
            if not consumer_tag:
                consumer_tag = self._impl._generate_consumer_tag()

            self._queue_consumer_generator = _QueueConsumerGeneratorInfo(
                params, consumer_tag)

            try:
                self._basic_consume_impl(
                    queue=queue,
                    auto_ack=auto_ack,
                    exclusive=exclusive,
                    consumer_tag=consumer_tag,
                    arguments=arguments,
                    alternate_event_sink=self._on_consumer_generator_event)
            except Exception:
                self._queue_consumer_generator = None
                raise

            LOGGER.info('Created new queue consumer generator %r',
                        self._queue_consumer_generator)

        while self._queue_consumer_generator is not None:
            # Process pending events
            if self._queue_consumer_generator.pending_events:
                evt = self._queue_consumer_generator.pending_events.popleft()
                if type(evt) is _ConsumerCancellationEvt:
                    # Consumer was cancelled by broker
                    self._queue_consumer_generator = None
                    break
                else:
                    yield (evt.method, evt.properties, evt.body)
                    continue

            if inactivity_timeout is None:
                # Wait indefinitely for a message to arrive, while processing
                # I/O events and triggering ChannelClosed exception when the
                # channel fails
                self._process_data_events(time_limit=None)
                continue

            # Wait with inactivity timeout
            wait_start_time = pika._utils.time_now()
            wait_deadline = wait_start_time + inactivity_timeout
            delta = inactivity_timeout

            while (self._queue_consumer_generator is not None and
                   not self._queue_consumer_generator.pending_events):

                self._process_data_events(time_limit=delta)

                if not self._queue_consumer_generator:
                    # Consumer was cancelled by client
                    break

                if self._queue_consumer_generator.pending_events:
                    # Got message(s)
                    break

                delta = wait_deadline - pika._utils.time_now()
                if delta <= 0.0:
                    # Signal inactivity timeout
                    yield (None, None, None)
                    break

    def _process_data_events(self, time_limit: float | None) -> None:
        """
        Wrapper for `BlockingConnection.process_data_events()` with common channel-specific logic
        that raises ChannelClosed if broker closed this channel.

        NOTE: We need to raise an exception in the context of user's call into
        our API to protect the integrity of the underlying implementation.
        BlockingConnection's underlying asynchronous connection adapter
        (SelectConnection) uses callbacks to communicate with us. If
        BlockingConnection leaks exceptions back into the I/O loop or the
        asynchronous connection adapter, we interrupt their normal workflow and
        introduce a high likelihood of state inconsistency.

        See `BlockingConnection.process_data_events()` for documentation of args
        and behavior.

        :param time_limit:
        """
        self.connection.process_data_events(time_limit=time_limit)
        if self.is_closed and isinstance(self._closing_reason,
                                         exceptions.ChannelClosedByBroker):
            LOGGER.debug('Channel close by broker detected, raising %r; %r',
                         self._closing_reason, self)
            raise self._closing_reason

    def get_waiting_message_count(self) -> int:
        """
        Returns the number of messages that may be retrieved from the current queue consumer
        generator via `BlockingChannel.consume` without blocking. NEW in pika 0.10.0.

        :returns: The number of waiting messages
        """
        if self._queue_consumer_generator is not None:
            pending_events = self._queue_consumer_generator.pending_events
            count = len(pending_events)
            if count and type(pending_events[-1]) is _ConsumerCancellationEvt:
                count -= 1
        else:
            count = 0

        return count

    def cancel(self) -> int:
        """
        Cancel the queue consumer created by `BlockingChannel.consume`, rejecting all pending
        ackable messages.

        NOTE: If you're looking to cancel a consumer issued with
        BlockingChannel.basic_consume then you should call
        BlockingChannel.basic_cancel.

        :returns: The number of messages requeued by Basic.Nack.
            NEW in 0.10.0: returns 0
        """
        if self._queue_consumer_generator is None:
            LOGGER.warning('cancel: queue consumer generator is inactive '
                           '(already cancelled by client or broker?)')
            return 0

        try:
            _, auto_ack, _ = self._queue_consumer_generator.params
            if not auto_ack:
                # Reject messages held by queue consumer generator; NOTE: we
                # can't use basic_nack with the multiple option to avoid nacking
                # messages already held by our client.
                pending_events = self._queue_consumer_generator.pending_events
                # NOTE `get_waiting_message_count` adjusts for `Basic.Cancel`
                #      from the server at the end (if any)
                for _ in range(self.get_waiting_message_count()):
                    evt = pending_events.popleft()
                    self._impl.basic_reject(evt.method.delivery_tag,
                                            requeue=True)

            self.basic_cancel(self._queue_consumer_generator.consumer_tag)
        finally:
            self._queue_consumer_generator = None

        # Return 0 for compatibility with legacy implementation; the number of
        # nacked messages is not meaningful since only messages consumed with
        # auto_ack=False may be nacked, and those arriving after calling
        # basic_cancel will be rejected automatically by impl channel, so we'll
        # never know how many of those were nacked.
        return 0

    def basic_ack(self, delivery_tag: int = 0, multiple: bool = False) -> None:
        """
        Acknowledge one or more messages.

        When sent by the client, this method acknowledges one or more messages delivered via the
        Deliver or Get-Ok methods. When sent by server, this method acknowledges one or more
        messages published with the Publish method on a channel in confirm mode. The acknowledgement
        can be for a single message or a set of messages up to and including a specific message.

        :param delivery_tag: The server-assigned delivery tag
        :param multiple: If set to True, the delivery tag is treated as "up to and including", so
            that multiple messages can be acknowledged with a single method. If set to False, the
            delivery tag refers to a single message. If the multiple field is 1, and the delivery
            tag is zero, this indicates acknowledgement of all outstanding messages.
        """
        self._impl.basic_ack(delivery_tag=delivery_tag, multiple=multiple)
        self._flush_output()

    def basic_nack(self,
                   delivery_tag: int = 0,
                   multiple: bool = False,
                   requeue: bool = True) -> None:
        """
        This method allows a client to reject one or more incoming messages.

        It can be used to interrupt and cancel large incoming messages, or return untreatable
        messages to their original queue.

        :param delivery_tag: The server-assigned delivery tag
        :param multiple: If set to True, the delivery tag is treated as "up to and including", so
            that multiple messages can be acknowledged with a single method. If set to False, the
            delivery tag refers to a single message. If the multiple field is 1, and the delivery
            tag is zero, this indicates acknowledgement of all outstanding messages.
        :param requeue: If requeue is true, the server will attempt to requeue the message. If
            requeue is false or the requeue attempt fails the messages are discarded or dead-
            lettered.
        """
        self._impl.basic_nack(delivery_tag=delivery_tag,
                              multiple=multiple,
                              requeue=requeue)
        self._flush_output()

    def basic_get(
        self,
        queue: str,
        auto_ack: bool = False
    ) -> tuple[pika.spec.Basic.GetOk | None, pika.spec.BasicProperties | None,
               bytes | None]:
        """
        Get a single message from the AMQP broker.

        Returns a sequence with the method frame, message properties, and body.

        :param queue: Name of queue from which to get a message
        :param auto_ack: Tell the broker to not expect a reply
        :returns: a three-tuple; (None, None, None) if the queue was empty; otherwise (method,
            properties, body); NOTE: body may be None
        """
        assert not self._basic_getempty_result

        validators.require_string(queue, 'queue')

        with contextlib.ExitStack() as stack:
            get_ok_result = stack.enter_context(
                _CallbackResult(self._RxMessageArgs))
            stack.enter_context(self._basic_getempty_result)
            self._impl.basic_get(queue=queue,
                                 auto_ack=auto_ack,
                                 callback=get_ok_result.set_value_once)
            self._flush_output(get_ok_result.is_ready,
                               self._basic_getempty_result.is_ready)
            if get_ok_result:
                evt = get_ok_result.value
                return evt.method, evt.properties, evt.body
            assert self._basic_getempty_result, (
                'wait completed without GetOk and GetEmpty')
            return None, None, None

    def basic_publish(self,
                      exchange: str,
                      routing_key: str,
                      body: bytes,
                      properties: pika.spec.BasicProperties | None = None,
                      mandatory: bool = False) -> None:
        """
        Publish to the channel with the given exchange, routing key, and body.

        For more information on basic_publish and what the parameters do, see:

            https://www.rabbitmq.com/amqp-0-9-1-reference.html#basic.publish

        NOTE: mandatory may be enabled even without delivery
          confirmation, but in the absence of delivery confirmation the
          synchronous implementation has no way to know how long to wait for
          the Basic.Return.

        :param exchange: The exchange to publish to
        :param routing_key: The routing key to bind on
        :param body: The message body; empty string if no body
        :param properties: message properties
        :param mandatory: The mandatory flag

        :raises UnroutableError: raised when a message published in
            publisher-acknowledgments mode (see
            `BlockingChannel.confirm_delivery`) is returned via `Basic.Return`
            followed by `Basic.Ack`.
        :raises NackError: raised when a message published in
            publisher-acknowledgements mode is Nack'ed by the broker. See
            `BlockingChannel.confirm_delivery`.
        """
        if self._delivery_confirmation:
            # In publisher-acknowledgments mode
            with self._message_confirmation_result:
                self._impl.basic_publish(exchange=exchange,
                                         routing_key=routing_key,
                                         body=body,
                                         properties=properties,
                                         mandatory=mandatory)

                self._flush_output(self._message_confirmation_result.is_ready)
                conf_method = (
                    self._message_confirmation_result.value.method_frame.method)

                if isinstance(conf_method, pika.spec.Basic.Nack):
                    # Broker was unable to process the message due to internal
                    # error
                    LOGGER.warning(
                        "Message was Nack'ed by broker: nack=%r; channel=%s; "
                        "exchange=%s; routing_key=%s; mandatory=%r; ",
                        conf_method, self.channel_number, exchange, routing_key,
                        mandatory)
                    if self._puback_return is not None:
                        returned_messages: list[ReturnedMessage] = [
                            self._puback_return
                        ]
                        self._puback_return = None
                    else:
                        returned_messages = []
                    raise exceptions.NackError(returned_messages)

                assert isinstance(conf_method,
                                  pika.spec.Basic.Ack), (conf_method)

                if self._puback_return is not None:
                    # Unroutable message was returned
                    messages = [self._puback_return]
                    self._puback_return = None
                    raise exceptions.UnroutableError(messages)
        else:
            # In non-publisher-acknowledgments mode
            self._impl.basic_publish(exchange=exchange,
                                     routing_key=routing_key,
                                     body=body,
                                     properties=properties,
                                     mandatory=mandatory)
            self._flush_output()

    def basic_qos(self,
                  prefetch_size: int = 0,
                  prefetch_count: int = 0,
                  global_qos: bool = False) -> None:
        """
        Specify quality of service.

        This method requests a specific quality of service. The QoS can be specified for the current
        channel or for all channels on the connection. The client can request that messages be sent
        in advance so that when the client finishes processing a message, the following message is
        already held locally, rather than needing to be sent down the channel. Prefetching gives a
        performance improvement.

        :param prefetch_size: This field specifies the prefetch window size. The server will send a
            message in advance if it is equal to or smaller in size than the available prefetch size
            (and also falls into other prefetch limits). May be set to zero, meaning "no specific
            limit", although other prefetch limits may still apply. The prefetch-size is ignored if
            the no-ack option is set in the consumer.
        :param prefetch_count: Specifies a prefetch window in terms of whole messages. This field
            may be used in combination with the prefetch-size field; a message will only be sent in
            advance if both prefetch windows (and those at the channel and connection level) allow
            it. The prefetch-count is ignored if the no-ack option is set in the consumer.
        :param global_qos: Should the QoS apply to all channels on the connection.
        """
        with _CallbackResult() as qos_ok_result:
            self._impl.basic_qos(callback=qos_ok_result.signal_once,
                                 prefetch_size=prefetch_size,
                                 prefetch_count=prefetch_count,
                                 global_qos=global_qos)
            self._flush_output(qos_ok_result.is_ready)

    def basic_recover(self, requeue: bool = False) -> None:
        """
        This method asks the server to redeliver all unacknowledged messages on a specified channel.
        Zero or more messages may be redelivered. This method replaces the asynchronous Recover.

        :param requeue: If False, the message will be redelivered to the original recipient. If
            True, the server will attempt to requeue the message, potentially then delivering it to
            an alternative subscriber.
        """
        with _CallbackResult() as recover_ok_result:
            self._impl.basic_recover(requeue=requeue,
                                     callback=recover_ok_result.signal_once)
            self._flush_output(recover_ok_result.is_ready)

    def basic_reject(self, delivery_tag: int = 0, requeue: bool = True) -> None:
        """
        Reject an incoming message.

        This method allows a client to reject a message. It can be used to interrupt and cancel
        large incoming messages, or return untreatable messages to their original queue.

        :param delivery_tag: The server-assigned delivery tag
        :param requeue: If requeue is true, the server will attempt to requeue the message. If
            requeue is false or the requeue attempt fails the messages are discarded or dead-
            lettered.
        """
        self._impl.basic_reject(delivery_tag=delivery_tag, requeue=requeue)
        self._flush_output()

    def confirm_delivery(self) -> None:
        """
        Turn on RabbitMQ-proprietary Confirm mode in the channel.

        For more information see:
            https://www.rabbitmq.com/confirms.html
        """
        if self._delivery_confirmation:
            LOGGER.error(
                'confirm_delivery: confirmation was already enabled '
                'on channel=%s', self.channel_number)
            return

        with _CallbackResult() as select_ok_result:
            self._impl.confirm_delivery(
                ack_nack_callback=self._message_confirmation_result.
                set_value_once,
                callback=select_ok_result.signal_once)

            self._flush_output(select_ok_result.is_ready)

        self._delivery_confirmation = True

        # Unroutable messages returned after this point will be in the context
        # of publisher acknowledgments
        self._impl.add_on_return_callback(self._on_puback_message_returned)

    def exchange_declare(self,
                         exchange: str,
                         exchange_type: (ExchangeType |
                                         str) = ExchangeType.direct,
                         passive: bool = False,
                         durable: bool = False,
                         auto_delete: bool = False,
                         internal: bool = False,
                         arguments: dict[str, Any] | None = None) -> None:
        """
        This method creates an exchange if it does not already exist, and if the exchange exists,
        verifies that it is of the correct and expected class.

        If passive set, the server will reply with Declare-Ok if the exchange
        already exists with the same name, and raise an error if not and if the
        exchange does not already exist, the server MUST raise a channel
        exception with reply code 404 (not found).

        :param exchange: The exchange name consists of a non-empty sequence of
                          these characters: letters, digits, hyphen, underscore,
                          period, or colon.
        :param exchange_type: The exchange type to use
        :param passive: Perform a declare or just check to see if it exists
        :param durable: Survive a reboot of RabbitMQ
        :param auto_delete: Remove when no more queues are bound to it
        :param internal: Can only be published to by other exchanges
        :param arguments: Custom key/value pair arguments for the exchange
        :returns: Method frame from the Exchange.Declare-ok response
        """
        validators.require_string(exchange, 'exchange')
        with _CallbackResult(
                self._MethodFrameCallbackResultArgs) as declare_ok_result:
            self._impl.exchange_declare(
                exchange=exchange,
                exchange_type=exchange_type,
                passive=passive,
                durable=durable,
                auto_delete=auto_delete,
                internal=internal,
                arguments=arguments,
                callback=declare_ok_result.set_value_once)

            self._flush_output(declare_ok_result.is_ready)
            return declare_ok_result.value.method_frame

    def exchange_delete(self,
                        exchange: str | None = None,
                        if_unused: bool = False
                       ) -> pika.frame.Method[pika.spec.Exchange.DeleteOk]:
        """
        Delete the exchange.

        :param exchange: The exchange name
        :param if_unused: only delete if the exchange is unused
        :returns: Method frame from the Exchange.Delete-ok response
        """
        with _CallbackResult(
                self._MethodFrameCallbackResultArgs) as delete_ok_result:
            self._impl.exchange_delete(exchange=exchange,
                                       if_unused=if_unused,
                                       callback=delete_ok_result.set_value_once)

            self._flush_output(delete_ok_result.is_ready)
            return delete_ok_result.value.method_frame

    def exchange_bind(
        self,
        destination: str,
        source: str,
        routing_key: str = '',
        arguments: dict[str, Any] | None = None
    ) -> pika.frame.Method[pika.spec.Exchange.BindOk]:
        """
        Bind an exchange to another exchange.

        :param destination: The destination exchange to bind
        :param source: The source exchange to bind to
        :param routing_key: The routing key to bind on
        :param arguments: Custom key/value pair arguments for the binding
        :returns: Method frame from the Exchange.Bind-ok response
        """
        validators.require_string(destination, 'destination')
        validators.require_string(source, 'source')
        with _CallbackResult(
                self._MethodFrameCallbackResultArgs) as bind_ok_result:
            self._impl.exchange_bind(destination=destination,
                                     source=source,
                                     routing_key=routing_key,
                                     arguments=arguments,
                                     callback=bind_ok_result.set_value_once)

            self._flush_output(bind_ok_result.is_ready)
            return bind_ok_result.value.method_frame

    def exchange_unbind(
        self,
        destination: str | None = None,
        source: str | None = None,
        routing_key: str = '',
        arguments: dict[str, Any] | None = None
    ) -> pika.frame.Method[pika.spec.Exchange.UnbindOk]:
        """
        Unbind an exchange from another exchange.

        :param destination: The destination exchange to unbind
        :param source: The source exchange to unbind from
        :param routing_key: The routing key to unbind
        :param arguments: Custom key/value pair arguments for the binding
        :returns: Method frame from the Exchange.Unbind-ok response
        """
        with _CallbackResult(
                self._MethodFrameCallbackResultArgs) as unbind_ok_result:
            self._impl.exchange_unbind(destination=destination,
                                       source=source,
                                       routing_key=routing_key,
                                       arguments=arguments,
                                       callback=unbind_ok_result.set_value_once)

            self._flush_output(unbind_ok_result.is_ready)
            return unbind_ok_result.value.method_frame

    def queue_declare(
        self,
        queue: str,
        passive: bool = False,
        durable: bool = False,
        exclusive: bool = False,
        auto_delete: bool = False,
        arguments: dict[str, Any] | None = None
    ) -> pika.frame.Method[pika.spec.Queue.DeclareOk]:
        """
        Declare queue, create if needed.

        This method creates or checks a queue. When creating a new queue the client can specify various
        properties that control the durability of the queue and its contents,
        and the level of sharing for the queue.

        Use an empty string as the queue name for the broker to auto-generate
        one. Retrieve this auto-generated queue name from the returned
        `spec.Queue.DeclareOk` method frame.

        :param queue: The queue name; if empty string, the broker will
            create a unique queue name
        :param passive: Only check to see if the queue exists and raise
          `ChannelClosed` if it doesn't
        :param durable: Survive reboots of the broker
        :param exclusive: Only allow access by the current connection
        :param auto_delete: Delete after consumer cancels or disconnects
        :param arguments: Custom key/value arguments for the queue
        :returns: Method frame from the Queue.Declare-ok response
        """
        validators.require_string(queue, 'queue')
        with _CallbackResult(
                self._MethodFrameCallbackResultArgs) as declare_ok_result:
            self._impl.queue_declare(queue=queue,
                                     passive=passive,
                                     durable=durable,
                                     exclusive=exclusive,
                                     auto_delete=auto_delete,
                                     arguments=arguments,
                                     callback=declare_ok_result.set_value_once)

            self._flush_output(declare_ok_result.is_ready)
            return declare_ok_result.value.method_frame

    def queue_delete(self,
                     queue: str,
                     if_unused: bool = False,
                     if_empty: bool = False
                    ) -> pika.frame.Method[pika.spec.Queue.DeleteOk]:
        """
        Delete a queue from the broker.

        :param queue: The queue to delete
        :param if_unused: only delete if it's unused
        :param if_empty: only delete if the queue is empty
        :returns: Method frame from the Queue.Delete-ok response
        """
        with _CallbackResult(
                self._MethodFrameCallbackResultArgs) as delete_ok_result:
            self._impl.queue_delete(queue=queue,
                                    if_unused=if_unused,
                                    if_empty=if_empty,
                                    callback=delete_ok_result.set_value_once)

            self._flush_output(delete_ok_result.is_ready)
            return delete_ok_result.value.method_frame

    def queue_purge(self,
                    queue: str) -> pika.frame.Method[pika.spec.Queue.PurgeOk]:
        """
        Purge all of the messages from the specified queue.

        :param queue: The queue to purge
        :returns: Method frame from the Queue.Purge-ok response
        """
        with _CallbackResult(
                self._MethodFrameCallbackResultArgs) as purge_ok_result:
            self._impl.queue_purge(queue=queue,
                                   callback=purge_ok_result.set_value_once)
            self._flush_output(purge_ok_result.is_ready)
            return purge_ok_result.value.method_frame

    def queue_bind(
        self,
        queue: str,
        exchange: str,
        routing_key: str | None = None,
        arguments: dict[str, Any] | None = None
    ) -> pika.frame.Method[pika.spec.Queue.BindOk]:
        """
        Bind the queue to the specified exchange.

        :param queue: The queue to bind to the exchange
        :param exchange: The source exchange to bind to
        :param routing_key: The routing key to bind on
        :param arguments: Custom key/value pair arguments for the binding
        :returns: Method frame from the Queue.Bind-ok response
        """
        validators.require_string(queue, 'queue')
        validators.require_string(exchange, 'exchange')
        with _CallbackResult(
                self._MethodFrameCallbackResultArgs) as bind_ok_result:
            self._impl.queue_bind(queue=queue,
                                  exchange=exchange,
                                  routing_key=routing_key,
                                  arguments=arguments,
                                  callback=bind_ok_result.set_value_once)
            self._flush_output(bind_ok_result.is_ready)
            return bind_ok_result.value.method_frame

    def queue_unbind(
        self,
        queue: str,
        exchange: str,
        routing_key: str | None = None,
        arguments: dict[str, Any] | None = None
    ) -> pika.frame.Method[pika.spec.Queue.UnbindOk]:
        """
        Unbind a queue from an exchange.

        :param queue: The queue to unbind from the exchange
        :param exchange: The source exchange to bind from
        :param routing_key: The routing key to unbind
        :param arguments: Custom key/value pair arguments for the binding
        :returns: Method frame from the Queue.Unbind-ok response
        """
        with _CallbackResult(
                self._MethodFrameCallbackResultArgs) as unbind_ok_result:
            self._impl.queue_unbind(queue=queue,
                                    exchange=exchange,
                                    routing_key=routing_key,
                                    arguments=arguments,
                                    callback=unbind_ok_result.set_value_once)
            self._flush_output(unbind_ok_result.is_ready)
            return unbind_ok_result.value.method_frame

    def tx_select(self) -> pika.frame.Method[pika.spec.Tx.SelectOk]:
        """
        Select standard transaction mode.

        This method sets the channel to use standard transactions. The client must use this method
        at least once on a channel before using the Commit or Rollback methods.

        :returns: Method frame from the Tx.Select-ok response
        """
        with _CallbackResult(
                self._MethodFrameCallbackResultArgs) as select_ok_result:
            self._impl.tx_select(select_ok_result.set_value_once)

            self._flush_output(select_ok_result.is_ready)
            return select_ok_result.value.method_frame

    def tx_commit(self) -> pika.frame.Method[pika.spec.Tx.CommitOk]:
        """
        Commit a transaction.

        :returns: Method frame from the Tx.Commit-ok response
        """
        with _CallbackResult(
                self._MethodFrameCallbackResultArgs) as commit_ok_result:
            self._impl.tx_commit(commit_ok_result.set_value_once)

            self._flush_output(commit_ok_result.is_ready)
            return commit_ok_result.value.method_frame

    def tx_rollback(self) -> pika.frame.Method[pika.spec.Tx.RollbackOk]:
        """
        Rollback a transaction.

        :returns: Method frame from the Tx.Rollback-ok response
        """
        with _CallbackResult(
                self._MethodFrameCallbackResultArgs) as rollback_ok_result:
            self._impl.tx_rollback(rollback_ok_result.set_value_once)

            self._flush_output(rollback_ok_result.is_ready)
            return rollback_ok_result.value.method_frame

_ALWAYS_READY_WAITERS class-attribute instance-attribute

_ALWAYS_READY_WAITERS = (lambda: True,)

_CONSUMER_CANCELLED_CB_KEY class-attribute instance-attribute

_CONSUMER_CANCELLED_CB_KEY = (
    'blocking_channel_consumer_cancelled'
)

_basic_consume_ok_result instance-attribute

_basic_consume_ok_result = _CallbackResult()

_basic_getempty_result instance-attribute

_basic_getempty_result = _CallbackResult(
    self._MethodFrameCallbackResultArgs
)

_closing_reason instance-attribute

_closing_reason = None

_connection instance-attribute

_connection = connection

_consumer_infos instance-attribute

_consumer_infos: dict[Any, Any] = {}

_delivery_confirmation instance-attribute

_delivery_confirmation = False

_impl instance-attribute

_impl = channel_impl

_message_confirmation_result instance-attribute

_message_confirmation_result = _CallbackResult(
    self._OnMessageConfirmationReportArgs
)

_pending_events instance-attribute

_pending_events: deque[Any] = deque()

_puback_return instance-attribute

_puback_return: ReturnedMessage | None = None

_queue_consumer_generator instance-attribute

_queue_consumer_generator: (
    _QueueConsumerGeneratorInfo | None
) = None

channel_number property

channel_number: int

Channel number.

connection property

connection: BlockingConnection

The channel's BlockingConnection instance.

consumer_tags property

consumer_tags: list[str]

Property method that returns a list of consumer tags for active consumers.

is_closed property

is_closed: bool

Returns True if the channel is closed.

is_open property

is_open: bool

Returns True if the channel is open.

_FlowOkCallbackResultArgs

Bases: NamedTuple

Source code in pika/adapters/blocking_connection.py
class _FlowOkCallbackResultArgs(NamedTuple):
    active: bool

_MethodFrameCallbackResultArgs

Bases: NamedTuple

Source code in pika/adapters/blocking_connection.py
class _MethodFrameCallbackResultArgs(NamedTuple):
    method_frame: Any

_OnMessageConfirmationReportArgs

Bases: NamedTuple

Source code in pika/adapters/blocking_connection.py
class _OnMessageConfirmationReportArgs(NamedTuple):
    method_frame: Any

_RxMessageArgs

Bases: NamedTuple

Source code in pika/adapters/blocking_connection.py
class _RxMessageArgs(NamedTuple):
    channel: Any
    method: Any
    properties: Any
    body: bytes

__enter__

__enter__() -> Self
Source code in pika/adapters/blocking_connection.py
def __enter__(self) -> Self:
    return self

__exit__

__exit__(
    exc_type: type[BaseException] | None,
    value: BaseException | None,
    traceback: TracebackType | None,
) -> None
Source code in pika/adapters/blocking_connection.py
def __exit__(self, exc_type: type[BaseException] | None,
             value: BaseException | None,
             traceback: TracebackType | None) -> None:
    if self.is_open:
        self.close()

__init__

__init__(
    channel_impl: Channel, connection: BlockingConnection
) -> None

Create a new instance of the Channel.

PARAMETER DESCRIPTION
channel_impl

Channel implementation object as returned from SelectConnection.channel()

TYPE: Channel

connection

The connection object

TYPE: BlockingConnection

Source code in pika/adapters/blocking_connection.py
def __init__(self, channel_impl: pika.channel.Channel,
             connection: BlockingConnection) -> None:
    """
    Create a new instance of the Channel.

    :param channel_impl: Channel implementation object as returned from
        SelectConnection.channel()
    :param connection: The connection object
    """
    self._impl = channel_impl
    self._connection = connection

    # A mapping of consumer tags to _ConsumerInfo for active consumers
    self._consumer_infos: dict[Any, Any] = {}

    # Queue consumer generator generator info of type
    # _QueueConsumerGeneratorInfo created by BlockingChannel.consume
    self._queue_consumer_generator: _QueueConsumerGeneratorInfo | None = None

    # Whether RabbitMQ delivery confirmation has been enabled
    self._delivery_confirmation = False

    # Receives message delivery confirmation report (Basic.ack or
    # Basic.nack) from broker when delivery confirmations are enabled
    self._message_confirmation_result = _CallbackResult(
        self._OnMessageConfirmationReportArgs)

    # deque of pending events: _ConsumerDeliveryEvt and
    # _ConsumerCancellationEvt objects that will be returned by
    # `BlockingChannel.get_event()`
    self._pending_events: deque[Any] = deque()

    # Holds a ReturnedMessage object representing a message received via
    # Basic.Return in publisher-acknowledgments mode.
    self._puback_return: ReturnedMessage | None = None

    # self._on_channel_closed() saves the reason exception here
    self._closing_reason = None  # type: None | Exception

    # Receives Basic.ConsumeOk reply from server
    self._basic_consume_ok_result = _CallbackResult()

    # Receives args from Basic.GetEmpty response
    #  https://www.rabbitmq.com/amqp-0-9-1-reference.html#basic.get
    self._basic_getempty_result = _CallbackResult(
        self._MethodFrameCallbackResultArgs)

    self._impl.add_on_cancel_callback(self._on_consumer_cancelled_by_broker)

    self._impl.add_callback(self._basic_consume_ok_result.signal_once,
                            replies=[pika.spec.Basic.ConsumeOk],
                            one_shot=False)

    self._impl.add_on_close_callback(self._on_channel_closed)

    self._impl.add_callback(self._basic_getempty_result.set_value_once,
                            replies=[pika.spec.Basic.GetEmpty],
                            one_shot=False)

    LOGGER.info('Created channel=%s', self.channel_number)

__int__

__int__() -> int

Return the channel object as its channel number.

NOTE: inherited from legacy BlockingConnection; might be error-prone; use channel_number property instead.

Source code in pika/adapters/blocking_connection.py
def __int__(self) -> int:
    """
    Return the channel object as its channel number.

    NOTE: inherited from legacy BlockingConnection; might be error-prone;
    use `channel_number` property instead.
    """
    return self.channel_number

__repr__

__repr__() -> str
Source code in pika/adapters/blocking_connection.py
@override
def __repr__(self) -> str:
    return f'<{self.__class__.__name__} impl={self._impl!r}>'

_add_pending_event

_add_pending_event(evt: _ChannelPendingEvt) -> None

Append an event to the channel's list of events that are ready for dispatch to user and signal our connection that this channel is ready for event dispatch.

PARAMETER DESCRIPTION
evt

an event derived from _ChannelPendingEvt

TYPE: _ChannelPendingEvt

Source code in pika/adapters/blocking_connection.py
def _add_pending_event(self, evt: _ChannelPendingEvt) -> None:
    """
    Append an event to the channel's list of events that are ready for dispatch to user and
    signal our connection that this channel is ready for event dispatch.

    :param evt: an event derived from _ChannelPendingEvt
    """
    self._pending_events.append(evt)
    self.connection._request_channel_dispatch(self.channel_number)

_basic_consume_impl

_basic_consume_impl(
    queue: str,
    auto_ack: bool,
    exclusive: bool,
    consumer_tag: str | None,
    arguments: dict[str, Any] | None = None,
    on_message_callback: Callable[
        [BlockingChannel, Deliver, BasicProperties, bytes],
        None,
    ]
    | None = None,
    alternate_event_sink: Callable[
        [_ChannelPendingEvt], None
    ]
    | None = None,
) -> str

The low-level implementation used by basic_consume and consume.

See basic_consume docstring for more info.

NOTE: exactly one of on_message_callback/alternate_event_sink musts be non-None.

This method has one additional parameter alternate_event_sink over the args described in basic_consume.

PARAMETER DESCRIPTION
queue

Queue name

TYPE: str

auto_ack

If True, the broker acknowledges messages automatically

TYPE: bool

exclusive

If True, restrict access to the current connection

TYPE: bool

consumer_tag

if specified, only the consumer with the given tag will be cancelled

TYPE: str | None

arguments

Custom key/value pair arguments for the consumer

TYPE: dict[str, Any] | None DEFAULT: None

on_message_callback

Callback for delivered messages

TYPE: Callable[[BlockingChannel, Deliver, BasicProperties, bytes], None] | None DEFAULT: None

alternate_event_sink

if specified, _ConsumerDeliveryEvt and _ConsumerCancellationEvt objects will be diverted to this callback instead of being deposited in the channel's _pending_events container. Signature: alternate_event_sink(evt)

TYPE: Callable[[_ChannelPendingEvt], None] | None DEFAULT: None

RAISES DESCRIPTION
pika.exceptions.DuplicateConsumerTag

if consumer with given consumer_tag is already present.

Source code in pika/adapters/blocking_connection.py
def _basic_consume_impl(
    self,
    queue: str,
    auto_ack: bool,
    exclusive: bool,
    consumer_tag: str | None,
    arguments: dict[str, Any] | None = None,
    on_message_callback: Callable[[
        BlockingChannel, pika.spec.Basic.Deliver, pika.spec.
        BasicProperties, bytes
    ], None] | None = None,
    alternate_event_sink: Callable[[_ChannelPendingEvt], None] | None = None
) -> str:
    """
    The low-level implementation used by `basic_consume` and `consume`.

    See `basic_consume` docstring for more info.

    NOTE: exactly one of on_message_callback/alternate_event_sink musts be
    non-None.

    This method has one additional parameter alternate_event_sink over the
    args described in `basic_consume`.

    :param queue: Queue name
    :param auto_ack: If True, the broker acknowledges messages automatically
    :param exclusive: If True, restrict access to the current connection
    :param consumer_tag: if specified, only the consumer with the given tag will be cancelled
    :param arguments: Custom key/value pair arguments for the consumer
    :param on_message_callback: Callback for delivered messages
    :param alternate_event_sink: if specified, _ConsumerDeliveryEvt
        and _ConsumerCancellationEvt objects will be diverted to this
        callback instead of being deposited in the channel's
        `_pending_events` container. Signature:
        alternate_event_sink(evt)

    :raises pika.exceptions.DuplicateConsumerTag: if consumer with given
        consumer_tag is already present.
    """
    if (on_message_callback is None) == (alternate_event_sink is None):
        raise ValueError(((
            'exactly one of on_message_callback/alternate_event_sink must '
            'be non-None'), on_message_callback, alternate_event_sink))

    if not consumer_tag:
        # Need a consumer tag to register consumer info before sending
        # request to broker, because I/O might dispatch incoming messages
        # immediately following Basic.Consume-ok before _flush_output
        # returns
        consumer_tag = self._impl._generate_consumer_tag()

    if consumer_tag in self._consumer_infos:
        raise exceptions.DuplicateConsumerTag(consumer_tag)

    # Create new consumer
    self._consumer_infos[consumer_tag] = _ConsumerInfo(
        consumer_tag,
        auto_ack=auto_ack,
        on_message_callback=on_message_callback,
        alternate_event_sink=alternate_event_sink)

    try:
        with self._basic_consume_ok_result as ok_result:
            tag = self._impl.basic_consume(
                on_message_callback=self._on_consumer_message_delivery,
                queue=queue,
                auto_ack=auto_ack,
                exclusive=exclusive,
                consumer_tag=consumer_tag,
                arguments=arguments)

            assert tag == consumer_tag, (tag, consumer_tag)

            self._flush_output(ok_result.is_ready)
    except Exception:
        # If channel was closed, self._consumer_infos will be empty
        if consumer_tag in self._consumer_infos:
            del self._consumer_infos[consumer_tag]
            # Schedule termination of connection.process_data_events using a
            # negative channel number
            self.connection._request_channel_dispatch(-self.channel_number)
        raise

    # NOTE: Consumer could get cancelled by broker immediately after opening
    # (e.g., queue getting deleted externally)
    if self._consumer_infos[consumer_tag].setting_up:
        self._consumer_infos[consumer_tag].state = _ConsumerInfo.ACTIVE

    return consumer_tag

_cancel_all_consumers

_cancel_all_consumers() -> None

Cancel all consumers.

NOTE: pending non-ackable messages will be lost; pending ackable messages will be rejected.

Source code in pika/adapters/blocking_connection.py
def _cancel_all_consumers(self) -> None:
    """
    Cancel all consumers.

    NOTE: pending non-ackable messages will be lost; pending ackable
    messages will be rejected.
    """
    if self._consumer_infos:
        LOGGER.debug('Cancelling %i consumers', len(self._consumer_infos))

        if self._queue_consumer_generator is not None:
            # Cancel queue consumer generator
            self.cancel()

        # Cancel consumers created via basic_consume
        for consumer_tag in list(self._consumer_infos.keys()):
            self.basic_cancel(consumer_tag)

_cleanup

_cleanup() -> None

Clean up members that might inhibit garbage collection.

Source code in pika/adapters/blocking_connection.py
def _cleanup(self) -> None:
    """Clean up members that might inhibit garbage collection."""
    self._message_confirmation_result.reset()
    self._pending_events = deque()
    self._consumer_infos = {}
    self._queue_consumer_generator = None

_dispatch_events

_dispatch_events() -> None

Called by BlockingConnection to dispatch pending events.

BlockingChannel schedules this callback via BlockingConnection._request_channel_dispatch

Source code in pika/adapters/blocking_connection.py
def _dispatch_events(self) -> None:
    """
    Called by BlockingConnection to dispatch pending events.

    `BlockingChannel` schedules this callback via `BlockingConnection._request_channel_dispatch`
    """
    while self._pending_events:
        evt = self._pending_events.popleft()

        if type(evt) is _ConsumerDeliveryEvt:
            consumer_info = self._consumer_infos[evt.method.consumer_tag]
            consumer_info.on_message_callback(self, evt.method,
                                              evt.properties, evt.body)

        elif type(evt) is _ConsumerCancellationEvt:
            del self._consumer_infos[evt.method_frame.method.consumer_tag]

            self._impl.callbacks.process(self.channel_number,
                                         self._CONSUMER_CANCELLED_CB_KEY,
                                         self, evt.method_frame)
        else:
            evt.dispatch()

_flush_output

_flush_output(*waiters: Callable[[], bool]) -> None

Flush output and process input while waiting for any of the given callbacks to return true. The wait is aborted upon channel-close or connection-close. Otherwise, processing continues until the output is flushed AND at least one of the callbacks returns true. If there are no callbacks, then processing ends when all output is flushed.

PARAMETER DESCRIPTION
waiters

sequence of zero or more callables taking no args and returning true when it's time to stop processing. Their results are OR'ed together. An empty sequence is treated as equivalent to a waiter always returning true.

TYPE: Callable[[], bool] DEFAULT: ()

Source code in pika/adapters/blocking_connection.py
def _flush_output(self, *waiters: Callable[[], bool]) -> None:
    """
    Flush output and process input while waiting for any of the given callbacks to return true.
    The wait is aborted upon channel-close or connection-close. Otherwise, processing continues
    until the output is flushed AND at least one of the callbacks returns true. If there are no
    callbacks, then processing ends when all output is flushed.

    :param waiters: sequence of zero or more callables taking no args and returning true when
        it's time to stop processing. Their results are OR'ed together. An empty sequence is
        treated as equivalent to a waiter always returning true.
    """
    if self.is_closed:
        self._impl._raise_if_not_open()

    if not waiters:
        waiters = self._ALWAYS_READY_WAITERS

    self._connection._flush_output(lambda: self.is_closed, *waiters)

    if self.is_closed and isinstance(self._closing_reason,
                                     exceptions.ChannelClosedByBroker):
        raise self._closing_reason

_on_channel_closed

_on_channel_closed(
    _channel: Channel, reason: Exception
) -> None

Callback from impl notifying us that the channel has been closed.

This may be as the result of user-, broker-, or internal connection clean-up initiated closing or meta-closing of the channel.

If it resulted from receiving Channel.Close from broker, we will expedite waking up of the event subsystem so that it may respond by raising ChannelClosed from user's context.

NOTE: We can't raise exceptions in callbacks in order to protect the integrity of the underlying implementation. BlockingConnection's underlying asynchronous connection adapter (SelectConnection) uses callbacks to communicate with us. If BlockingConnection leaks exceptions back into the I/O loop or the asynchronous connection adapter, we interrupt their normal workflow and introduce a high likelihood of state inconsistency.

See pika.Channel.add_on_close_callback() for additional documentation.

PARAMETER DESCRIPTION
_channel

(unused)

TYPE: Channel

reason

TYPE: Exception

Source code in pika/adapters/blocking_connection.py
def _on_channel_closed(self, _channel: pika.channel.Channel,
                       reason: Exception) -> None:
    """
    Callback from impl notifying us that the channel has been closed.

    This may be as the result of user-, broker-, or internal connection clean-up initiated closing or meta-closing of the
    channel.

    If it resulted from receiving `Channel.Close` from broker, we will
    expedite waking up of the event subsystem so that it may respond by
    raising `ChannelClosed` from user's context.

    NOTE: We can't raise exceptions in callbacks in order to protect
    the integrity of the underlying implementation. BlockingConnection's
    underlying asynchronous connection adapter (SelectConnection) uses
    callbacks to communicate with us. If BlockingConnection leaks exceptions
    back into the I/O loop or the asynchronous connection adapter, we
    interrupt their normal workflow and introduce a high likelihood of state
    inconsistency.

    See `pika.Channel.add_on_close_callback()` for additional documentation.

    :param _channel: (unused)
    :param reason:
    """
    LOGGER.debug('_on_channel_closed: %r; %r', reason, self)

    self._closing_reason = reason

    if isinstance(reason, exceptions.ChannelClosedByBroker):
        self.connection._server_channel_closures.append(reason)
        self._cleanup()

        # Request urgent termination of `process_data_events()`, in case
        # it's executing or next time it will execute
        self.connection._request_channel_dispatch(-self.channel_number)

_on_consumer_cancelled_by_broker

_on_consumer_cancelled_by_broker(
    method_frame: Method[Cancel],
) -> None

Called by impl when broker cancels consumer via Basic.Cancel.

This is a RabbitMQ-specific feature. The circumstances include deletion of queue being consumed as well as failure of a HA node responsible for the queue being consumed.

PARAMETER DESCRIPTION
method_frame

method frame with the spec.Basic.Cancel method

TYPE: Method[Cancel]

Source code in pika/adapters/blocking_connection.py
def _on_consumer_cancelled_by_broker(
        self,
        method_frame: pika.frame.Method[pika.spec.Basic.Cancel]) -> None:
    """
    Called by impl when broker cancels consumer via Basic.Cancel.

    This is a RabbitMQ-specific feature. The circumstances include deletion of queue being
    consumed as well as failure of a HA node responsible for the queue being consumed.

    :param method_frame: method frame with the `spec.Basic.Cancel` method
    """
    evt = _ConsumerCancellationEvt(method_frame)

    consumer = self._consumer_infos[method_frame.method.consumer_tag]

    # Don't interfere with client-initiated cancellation flow
    if not consumer.tearing_down:
        consumer.state = _ConsumerInfo.CANCELLED_BY_BROKER

    if consumer.alternate_event_sink is not None:
        consumer.alternate_event_sink(evt)
    else:
        self._add_pending_event(evt)

_on_consumer_generator_event

_on_consumer_generator_event(
    evt: _ChannelPendingEvt,
) -> None

Sink for the queue consumer generator's consumer events; append the event to queue consumer generator's pending events buffer.

PARAMETER DESCRIPTION
evt

an object of type _ConsumerDeliveryEvt or _ConsumerCancellationEvt

TYPE: _ChannelPendingEvt

Source code in pika/adapters/blocking_connection.py
def _on_consumer_generator_event(self, evt: _ChannelPendingEvt) -> None:
    """
    Sink for the queue consumer generator's consumer events; append the event to queue consumer
    generator's pending events buffer.

    :param evt: an object of type _ConsumerDeliveryEvt or _ConsumerCancellationEvt
    """
    assert self._queue_consumer_generator is not None
    self._queue_consumer_generator.pending_events.append(evt)
    # Schedule termination of connection.process_data_events using a
    # negative channel number
    self.connection._request_channel_dispatch(-self.channel_number)

_on_consumer_message_delivery

_on_consumer_message_delivery(
    _channel: Channel,
    method: Deliver,
    properties: BasicProperties,
    body: bytes,
) -> None

Called by impl when a message is delivered for a consumer.

PARAMETER DESCRIPTION
_channel

The implementation channel object

TYPE: Channel

method

TYPE: Deliver

properties

message properties

TYPE: BasicProperties

body

delivered message body; empty string if no body

TYPE: bytes

Source code in pika/adapters/blocking_connection.py
def _on_consumer_message_delivery(self, _channel: pika.channel.Channel,
                                  method: pika.spec.Basic.Deliver,
                                  properties: pika.spec.BasicProperties,
                                  body: bytes) -> None:
    """
    Called by impl when a message is delivered for a consumer.

    :param _channel: The implementation channel object
    :param method:
    :param properties: message properties
    :param body: delivered message body; empty string if no body
    """
    evt = _ConsumerDeliveryEvt(method, properties, body)

    consumer = self._consumer_infos[method.consumer_tag]

    if consumer.alternate_event_sink is not None:
        consumer.alternate_event_sink(evt)
    else:
        self._add_pending_event(evt)

_on_puback_message_returned

_on_puback_message_returned(
    channel: Channel,
    method: Return,
    properties: BasicProperties,
    body: bytes,
) -> None

Called as the result of Basic.Return from broker in publisher-acknowledgements mode.

Saves the info as a ReturnedMessage instance in self._puback_return.

PARAMETER DESCRIPTION
channel

our self._impl channel

TYPE: Channel

method

TYPE: Return

properties

message properties

TYPE: BasicProperties

body

returned message body; empty string if no body

TYPE: bytes

Source code in pika/adapters/blocking_connection.py
def _on_puback_message_returned(self, channel: pika.channel.Channel,
                                method: pika.spec.Basic.Return,
                                properties: pika.spec.BasicProperties,
                                body: bytes) -> None:
    """
    Called as the result of Basic.Return from broker in publisher-acknowledgements mode.

    Saves the info as a ReturnedMessage instance in self._puback_return.

    :param channel: our self._impl channel
    :param method:
    :param properties: message properties
    :param body: returned message body; empty string if no body
    """
    assert channel is self._impl, (channel.channel_number,
                                   self.channel_number)

    assert isinstance(method, pika.spec.Basic.Return), method
    assert isinstance(properties, pika.spec.BasicProperties), (properties)

    LOGGER.warning(
        'Published message was returned: _delivery_confirmation=%s; '
        'channel=%s; method=%r; properties=%r; body_size=%d; '
        'body_prefix=%.255r', self._delivery_confirmation,
        channel.channel_number, method, properties,
        len(body) if body is not None else None, body)

    self._puback_return = ReturnedMessage(method, properties, body)

_process_data_events

_process_data_events(time_limit: float | None) -> None

Wrapper for BlockingConnection.process_data_events() with common channel-specific logic that raises ChannelClosed if broker closed this channel.

NOTE: We need to raise an exception in the context of user's call into our API to protect the integrity of the underlying implementation. BlockingConnection's underlying asynchronous connection adapter (SelectConnection) uses callbacks to communicate with us. If BlockingConnection leaks exceptions back into the I/O loop or the asynchronous connection adapter, we interrupt their normal workflow and introduce a high likelihood of state inconsistency.

See BlockingConnection.process_data_events() for documentation of args and behavior.

PARAMETER DESCRIPTION
time_limit

TYPE: float | None

Source code in pika/adapters/blocking_connection.py
def _process_data_events(self, time_limit: float | None) -> None:
    """
    Wrapper for `BlockingConnection.process_data_events()` with common channel-specific logic
    that raises ChannelClosed if broker closed this channel.

    NOTE: We need to raise an exception in the context of user's call into
    our API to protect the integrity of the underlying implementation.
    BlockingConnection's underlying asynchronous connection adapter
    (SelectConnection) uses callbacks to communicate with us. If
    BlockingConnection leaks exceptions back into the I/O loop or the
    asynchronous connection adapter, we interrupt their normal workflow and
    introduce a high likelihood of state inconsistency.

    See `BlockingConnection.process_data_events()` for documentation of args
    and behavior.

    :param time_limit:
    """
    self.connection.process_data_events(time_limit=time_limit)
    if self.is_closed and isinstance(self._closing_reason,
                                     exceptions.ChannelClosedByBroker):
        LOGGER.debug('Channel close by broker detected, raising %r; %r',
                     self._closing_reason, self)
        raise self._closing_reason

_remove_pending_deliveries

_remove_pending_deliveries(
    consumer_tag: str,
) -> Sequence[_ConsumerDeliveryEvt]

Extract _ConsumerDeliveryEvt objects destined for the given consumer from pending events, discarding the _ConsumerCancellationEvt, if any.

PARAMETER DESCRIPTION
consumer_tag

TYPE: str

RETURNS DESCRIPTION
Sequence[_ConsumerDeliveryEvt]

a (possibly empty) sequence of _ConsumerDeliveryEvt destined for the given consumer tag

Source code in pika/adapters/blocking_connection.py
def _remove_pending_deliveries(
        self, consumer_tag: str) -> Sequence[_ConsumerDeliveryEvt]:
    """
    Extract _ConsumerDeliveryEvt objects destined for the given consumer from pending events,
    discarding the _ConsumerCancellationEvt, if any.

    :param consumer_tag:

    :returns: a (possibly empty) sequence of _ConsumerDeliveryEvt destined for the given
        consumer tag
    """
    remaining_events: deque[Any] = deque()
    unprocessed_messages: list[Any] = []
    while self._pending_events:
        evt = self._pending_events.popleft()
        if (type(evt) is _ConsumerDeliveryEvt and
                evt.method.consumer_tag == consumer_tag):
            unprocessed_messages.append(evt)
            continue
        if (type(evt) is _ConsumerCancellationEvt and
                evt.method_frame.method.consumer_tag == consumer_tag):
            # A broker-initiated Basic.Cancel must have arrived
            # before our cancel request completed
            continue

        remaining_events.append(evt)

    self._pending_events = remaining_events

    return unprocessed_messages

add_on_cancel_callback

add_on_cancel_callback(
    callback: Callable[[Method[Cancel]], None],
) -> None

Pass a callback function that will be called when Basic.Cancel is sent by the broker.

The callback function should receive a method frame parameter.

PARAMETER DESCRIPTION
callback

a callable for handling broker's Basic.Cancel notification with the call signature: callback(method_frame) where method_frame is of type pika.frame.Method with method of type spec.Basic.Cancel

TYPE: Callable[[Method[Cancel]], None]

Source code in pika/adapters/blocking_connection.py
def add_on_cancel_callback(
    self, callback: Callable[[pika.frame.Method[pika.spec.Basic.Cancel]],
                             None]
) -> None:
    """
    Pass a callback function that will be called when Basic.Cancel is sent by the broker.

    The callback function should receive a method frame parameter.

    :param callback: a callable for handling broker's Basic.Cancel
        notification with the call signature: callback(method_frame)
        where method_frame is of type `pika.frame.Method` with method of
        type `spec.Basic.Cancel`
    """
    self._impl.callbacks.add(self.channel_number,
                             self._CONSUMER_CANCELLED_CB_KEY,
                             callback,
                             one_shot=False)

add_on_return_callback

add_on_return_callback(
    callback: Callable[
        [BlockingChannel, Return, BasicProperties, bytes],
        None,
    ],
) -> None

Pass a callback function that will be called when a published message is rejected and returned by the server via Basic.Return.

PARAMETER DESCRIPTION
callback

The method to call on callback with the signature callback(channel, method, properties, body), where - channel: pika.Channel - method: pika.spec.Basic.Return - properties: pika.spec.BasicProperties - body: bytes

TYPE: Callable[[BlockingChannel, Return, BasicProperties, bytes], None]

Source code in pika/adapters/blocking_connection.py
def add_on_return_callback(
    self, callback: Callable[[
        BlockingChannel, pika.spec.Basic.Return, pika.spec.
        BasicProperties, bytes
    ], None]
) -> None:
    """
    Pass a callback function that will be called when a published message is rejected and
    returned by the server via `Basic.Return`.

    :param callback: The method to call on callback with the
        signature callback(channel, method, properties, body), where
        - channel: pika.Channel
        - method: pika.spec.Basic.Return
        - properties: pika.spec.BasicProperties
        - body: bytes
    """
    self._impl.add_on_return_callback(
        lambda _channel, method, properties, body: (self._add_pending_event(
            _ReturnedMessageEvt(callback, self, method, properties, body))))

basic_ack

basic_ack(
    delivery_tag: int = 0, multiple: bool = False
) -> None

Acknowledge one or more messages.

When sent by the client, this method acknowledges one or more messages delivered via the Deliver or Get-Ok methods. When sent by server, this method acknowledges one or more messages published with the Publish method on a channel in confirm mode. The acknowledgement can be for a single message or a set of messages up to and including a specific message.

PARAMETER DESCRIPTION
delivery_tag

The server-assigned delivery tag

TYPE: int DEFAULT: 0

multiple

If set to True, the delivery tag is treated as "up to and including", so that multiple messages can be acknowledged with a single method. If set to False, the delivery tag refers to a single message. If the multiple field is 1, and the delivery tag is zero, this indicates acknowledgement of all outstanding messages.

TYPE: bool DEFAULT: False

Source code in pika/adapters/blocking_connection.py
def basic_ack(self, delivery_tag: int = 0, multiple: bool = False) -> None:
    """
    Acknowledge one or more messages.

    When sent by the client, this method acknowledges one or more messages delivered via the
    Deliver or Get-Ok methods. When sent by server, this method acknowledges one or more
    messages published with the Publish method on a channel in confirm mode. The acknowledgement
    can be for a single message or a set of messages up to and including a specific message.

    :param delivery_tag: The server-assigned delivery tag
    :param multiple: If set to True, the delivery tag is treated as "up to and including", so
        that multiple messages can be acknowledged with a single method. If set to False, the
        delivery tag refers to a single message. If the multiple field is 1, and the delivery
        tag is zero, this indicates acknowledgement of all outstanding messages.
    """
    self._impl.basic_ack(delivery_tag=delivery_tag, multiple=multiple)
    self._flush_output()

basic_cancel

basic_cancel(
    consumer_tag: str,
) -> Sequence[tuple[Deliver, BasicProperties, bytes]]

This method cancels a consumer.

This does not affect already delivered messages, but it does mean the server will not send any more messages for that consumer. The client may receive an arbitrary number of messages in between sending the cancel method and receiving the cancel-ok reply.

NOTE: When cancelling an auto_ack=False consumer, this implementation automatically Nacks and suppresses any incoming messages that have not yet been dispatched to the consumer's callback. However, when cancelling a auto_ack=True consumer, this method will return any pending messages that arrived before broker confirmed the cancellation.

PARAMETER DESCRIPTION
consumer_tag

Identifier for the consumer; the result of passing a consumer_tag that was created on another channel is undefined (bad things will happen)

TYPE: str

RETURNS DESCRIPTION
Sequence[tuple[Deliver, BasicProperties, bytes]]

(NEW IN pika 0.10.0) empty sequence for a auto_ack=False consumer; for a auto_ack=True consumer, returns a (possibly empty) sequence of pending messages that arrived before broker confirmed the cancellation (this is done instead of via consumer's callback in order to prevent reentrancy/recursion. Each message is three-tuple: (method, properties, body) - method: spec.Basic.Deliver - properties: spec.BasicProperties - body: bytes

Source code in pika/adapters/blocking_connection.py
def basic_cancel(
    self, consumer_tag: str
) -> Sequence[tuple[pika.spec.Basic.Deliver, pika.spec.BasicProperties,
                    bytes]]:
    """
    This method cancels a consumer.

    This does not affect already delivered messages, but it does mean the server will not send any more
    messages for that consumer. The client may receive an arbitrary number
    of messages in between sending the cancel method and receiving the
    cancel-ok reply.

    NOTE: When cancelling an auto_ack=False consumer, this implementation
    automatically Nacks and suppresses any incoming messages that have not
    yet been dispatched to the consumer's callback. However, when cancelling
    a auto_ack=True consumer, this method will return any pending messages
    that arrived before broker confirmed the cancellation.

    :param consumer_tag: Identifier for the consumer; the result of
        passing a consumer_tag that was created on another channel is
        undefined (bad things will happen)
    :returns: (NEW IN pika 0.10.0) empty sequence for a auto_ack=False
        consumer; for a auto_ack=True consumer, returns a (possibly empty)
        sequence of pending messages that arrived before broker confirmed
        the cancellation (this is done instead of via consumer's callback in
        order to prevent reentrancy/recursion. Each message is three-tuple:
        (method, properties, body)
        - method: spec.Basic.Deliver
        - properties: spec.BasicProperties
        - body: bytes
    """
    try:
        consumer_info = self._consumer_infos[consumer_tag]
    except KeyError:
        LOGGER.warning(
            'User is attempting to cancel an unknown consumer=%s; '
            'already cancelled by user or broker?', consumer_tag)
        return []

    try:
        # Assertion failure here is most likely due to reentrance
        assert consumer_info.active or consumer_info.cancelled_by_broker, (
            consumer_info.state)

        # Assertion failure here signals disconnect between consumer state
        # in BlockingChannel and Channel
        assert (consumer_info.cancelled_by_broker or
                consumer_tag in self._impl._consumers), consumer_tag

        auto_ack = consumer_info.auto_ack

        consumer_info.state = _ConsumerInfo.TEARING_DOWN

        with _CallbackResult() as cancel_ok_result:
            # Nack pending messages for auto_ack=False consumer
            if not auto_ack:
                pending_messages = self._remove_pending_deliveries(
                    consumer_tag)
                if pending_messages:
                    # NOTE: we use impl's basic_reject to avoid the
                    # possibility of redelivery before basic_cancel takes
                    # control of nacking.
                    # NOTE: we can't use basic_nack with the multiple option
                    # to avoid nacking messages already held by our client.
                    for message in pending_messages:
                        delivery_tag = message.method.delivery_tag
                        # The generated spec types delivery_tag as
                        # optional, but the broker always sets it on a
                        # delivery.
                        assert delivery_tag is not None
                        self._impl.basic_reject(delivery_tag, requeue=True)

            # Cancel the consumer; impl takes care of rejecting any
            # additional deliveries that arrive for a auto_ack=False
            # consumer
            self._impl.basic_cancel(consumer_tag=consumer_tag,
                                    callback=cancel_ok_result.signal_once)

            # Flush output and wait for Basic.Cancel-ok or
            # broker-initiated Basic.Cancel
            self._flush_output(
                cancel_ok_result.is_ready,
                lambda: consumer_tag not in self._impl._consumers)

        if auto_ack:
            # Return pending messages for auto_ack=True consumer
            return [(evt.method, evt.properties, evt.body)
                    for evt in self._remove_pending_deliveries(consumer_tag)
                   ]
        # impl takes care of rejecting any incoming deliveries during
        # cancellation
        messages = self._remove_pending_deliveries(consumer_tag)
        assert not messages, messages

        return []
    finally:
        # NOTE: The entry could be purged if channel or connection closes
        if consumer_tag in self._consumer_infos:
            del self._consumer_infos[consumer_tag]
            # Schedule termination of connection.process_data_events using a
            # negative channel number
            self.connection._request_channel_dispatch(-self.channel_number)

basic_consume

basic_consume(
    queue: str,
    on_message_callback: Callable[
        [BlockingChannel, Deliver, BasicProperties, bytes],
        None,
    ],
    auto_ack: bool = False,
    exclusive: bool = False,
    consumer_tag: str | None = None,
    arguments: dict[str, Any] | None = None,
) -> str

Sends the AMQP command Basic.Consume to the broker and binds messages for the consumer_tag to the consumer callback. If you do not pass in a consumer_tag, one will be automatically generated for you. Returns the consumer tag.

NOTE: the consumer callbacks are dispatched only in the scope of specially-designated methods: see BlockingConnection.process_data_events and BlockingChannel.start_consuming.

For more information about Basic.Consume, see: https://www.rabbitmq.com/amqp-0-9-1-reference.html#basic.consume

PARAMETER DESCRIPTION
queue

The queue from which to consume

TYPE: str

on_message_callback

Required function for dispatching messages to user, having the signature: on_message_callback(channel, method, properties, body) - channel: BlockingChannel - method: spec.Basic.Deliver - properties: spec.BasicProperties - body: bytes

TYPE: Callable[[BlockingChannel, Deliver, BasicProperties, bytes], None]

auto_ack

if set to True, automatic acknowledgement mode will be used (see https://www.rabbitmq.com/confirms.html). This corresponds with the 'no_ack' parameter in the basic.consume AMQP 0.9.1 method

TYPE: bool DEFAULT: False

exclusive

Don't allow other consumers on the queue

TYPE: bool DEFAULT: False

consumer_tag

You may specify your own consumer tag; if left empty, a consumer tag will be generated automatically

TYPE: str | None DEFAULT: None

arguments

Custom key/value pair arguments for the consumer

TYPE: dict[str, Any] | None DEFAULT: None

RETURNS DESCRIPTION
str

consumer tag

RAISES DESCRIPTION
pika.exceptions.DuplicateConsumerTag

if consumer with given consumer_tag is already present.

Source code in pika/adapters/blocking_connection.py
def basic_consume(self,
                  queue: str,
                  on_message_callback: Callable[[
                      BlockingChannel, pika.spec.Basic.Deliver, pika.spec.
                      BasicProperties, bytes
                  ], None],
                  auto_ack: bool = False,
                  exclusive: bool = False,
                  consumer_tag: str | None = None,
                  arguments: dict[str, Any] | None = None) -> str:
    """
    Sends the AMQP command Basic.Consume to the broker and binds messages for the consumer_tag
    to the consumer callback. If you do not pass in a consumer_tag, one will be automatically
    generated for you. Returns the consumer tag.

    NOTE: the consumer callbacks are dispatched only in the scope of
    specially-designated methods: see
    `BlockingConnection.process_data_events` and
    `BlockingChannel.start_consuming`.

    For more information about Basic.Consume, see:
    https://www.rabbitmq.com/amqp-0-9-1-reference.html#basic.consume

    :param queue: The queue from which to consume
    :param on_message_callback: Required function for dispatching messages
        to user, having the signature:
        on_message_callback(channel, method, properties, body)
        - channel: BlockingChannel
        - method: spec.Basic.Deliver
        - properties: spec.BasicProperties
        - body: bytes
    :param auto_ack: if set to True, automatic acknowledgement mode will be used
                          (see https://www.rabbitmq.com/confirms.html). This corresponds
                          with the 'no_ack' parameter in the basic.consume AMQP 0.9.1
                          method
    :param exclusive: Don't allow other consumers on the queue
    :param consumer_tag: You may specify your own consumer tag; if left
      empty, a consumer tag will be generated automatically
    :param arguments: Custom key/value pair arguments for the consumer
    :returns: consumer tag
    :raises pika.exceptions.DuplicateConsumerTag: if consumer with given
        consumer_tag is already present.
    """
    validators.require_string(queue, 'queue')
    validators.require_callback(on_message_callback, 'on_message_callback')
    return self._basic_consume_impl(queue=queue,
                                    on_message_callback=on_message_callback,
                                    auto_ack=auto_ack,
                                    exclusive=exclusive,
                                    consumer_tag=consumer_tag,
                                    arguments=arguments)

basic_get

basic_get(
    queue: str, auto_ack: bool = False
) -> tuple[
    GetOk | None, BasicProperties | None, bytes | None
]

Get a single message from the AMQP broker.

Returns a sequence with the method frame, message properties, and body.

PARAMETER DESCRIPTION
queue

Name of queue from which to get a message

TYPE: str

auto_ack

Tell the broker to not expect a reply

TYPE: bool DEFAULT: False

RETURNS DESCRIPTION
tuple[GetOk | None, BasicProperties | None, bytes | None]

a three-tuple; (None, None, None) if the queue was empty; otherwise (method, properties, body); NOTE: body may be None

Source code in pika/adapters/blocking_connection.py
def basic_get(
    self,
    queue: str,
    auto_ack: bool = False
) -> tuple[pika.spec.Basic.GetOk | None, pika.spec.BasicProperties | None,
           bytes | None]:
    """
    Get a single message from the AMQP broker.

    Returns a sequence with the method frame, message properties, and body.

    :param queue: Name of queue from which to get a message
    :param auto_ack: Tell the broker to not expect a reply
    :returns: a three-tuple; (None, None, None) if the queue was empty; otherwise (method,
        properties, body); NOTE: body may be None
    """
    assert not self._basic_getempty_result

    validators.require_string(queue, 'queue')

    with contextlib.ExitStack() as stack:
        get_ok_result = stack.enter_context(
            _CallbackResult(self._RxMessageArgs))
        stack.enter_context(self._basic_getempty_result)
        self._impl.basic_get(queue=queue,
                             auto_ack=auto_ack,
                             callback=get_ok_result.set_value_once)
        self._flush_output(get_ok_result.is_ready,
                           self._basic_getempty_result.is_ready)
        if get_ok_result:
            evt = get_ok_result.value
            return evt.method, evt.properties, evt.body
        assert self._basic_getempty_result, (
            'wait completed without GetOk and GetEmpty')
        return None, None, None

basic_nack

basic_nack(
    delivery_tag: int = 0,
    multiple: bool = False,
    requeue: bool = True,
) -> None

This method allows a client to reject one or more incoming messages.

It can be used to interrupt and cancel large incoming messages, or return untreatable messages to their original queue.

PARAMETER DESCRIPTION
delivery_tag

The server-assigned delivery tag

TYPE: int DEFAULT: 0

multiple

If set to True, the delivery tag is treated as "up to and including", so that multiple messages can be acknowledged with a single method. If set to False, the delivery tag refers to a single message. If the multiple field is 1, and the delivery tag is zero, this indicates acknowledgement of all outstanding messages.

TYPE: bool DEFAULT: False

requeue

If requeue is true, the server will attempt to requeue the message. If requeue is false or the requeue attempt fails the messages are discarded or dead- lettered.

TYPE: bool DEFAULT: True

Source code in pika/adapters/blocking_connection.py
def basic_nack(self,
               delivery_tag: int = 0,
               multiple: bool = False,
               requeue: bool = True) -> None:
    """
    This method allows a client to reject one or more incoming messages.

    It can be used to interrupt and cancel large incoming messages, or return untreatable
    messages to their original queue.

    :param delivery_tag: The server-assigned delivery tag
    :param multiple: If set to True, the delivery tag is treated as "up to and including", so
        that multiple messages can be acknowledged with a single method. If set to False, the
        delivery tag refers to a single message. If the multiple field is 1, and the delivery
        tag is zero, this indicates acknowledgement of all outstanding messages.
    :param requeue: If requeue is true, the server will attempt to requeue the message. If
        requeue is false or the requeue attempt fails the messages are discarded or dead-
        lettered.
    """
    self._impl.basic_nack(delivery_tag=delivery_tag,
                          multiple=multiple,
                          requeue=requeue)
    self._flush_output()

basic_publish

basic_publish(
    exchange: str,
    routing_key: str,
    body: bytes,
    properties: BasicProperties | None = None,
    mandatory: bool = False,
) -> None

Publish to the channel with the given exchange, routing key, and body.

For more information on basic_publish and what the parameters do, see:

https://www.rabbitmq.com/amqp-0-9-1-reference.html#basic.publish

NOTE: mandatory may be enabled even without delivery confirmation, but in the absence of delivery confirmation the synchronous implementation has no way to know how long to wait for the Basic.Return.

PARAMETER DESCRIPTION
exchange

The exchange to publish to

TYPE: str

routing_key

The routing key to bind on

TYPE: str

body

The message body; empty string if no body

TYPE: bytes

properties

message properties

TYPE: BasicProperties | None DEFAULT: None

mandatory

The mandatory flag

TYPE: bool DEFAULT: False

RAISES DESCRIPTION
UnroutableError

raised when a message published in publisher-acknowledgments mode (see BlockingChannel.confirm_delivery) is returned via Basic.Return followed by Basic.Ack.

NackError

raised when a message published in publisher-acknowledgements mode is Nack'ed by the broker. See BlockingChannel.confirm_delivery.

Source code in pika/adapters/blocking_connection.py
def basic_publish(self,
                  exchange: str,
                  routing_key: str,
                  body: bytes,
                  properties: pika.spec.BasicProperties | None = None,
                  mandatory: bool = False) -> None:
    """
    Publish to the channel with the given exchange, routing key, and body.

    For more information on basic_publish and what the parameters do, see:

        https://www.rabbitmq.com/amqp-0-9-1-reference.html#basic.publish

    NOTE: mandatory may be enabled even without delivery
      confirmation, but in the absence of delivery confirmation the
      synchronous implementation has no way to know how long to wait for
      the Basic.Return.

    :param exchange: The exchange to publish to
    :param routing_key: The routing key to bind on
    :param body: The message body; empty string if no body
    :param properties: message properties
    :param mandatory: The mandatory flag

    :raises UnroutableError: raised when a message published in
        publisher-acknowledgments mode (see
        `BlockingChannel.confirm_delivery`) is returned via `Basic.Return`
        followed by `Basic.Ack`.
    :raises NackError: raised when a message published in
        publisher-acknowledgements mode is Nack'ed by the broker. See
        `BlockingChannel.confirm_delivery`.
    """
    if self._delivery_confirmation:
        # In publisher-acknowledgments mode
        with self._message_confirmation_result:
            self._impl.basic_publish(exchange=exchange,
                                     routing_key=routing_key,
                                     body=body,
                                     properties=properties,
                                     mandatory=mandatory)

            self._flush_output(self._message_confirmation_result.is_ready)
            conf_method = (
                self._message_confirmation_result.value.method_frame.method)

            if isinstance(conf_method, pika.spec.Basic.Nack):
                # Broker was unable to process the message due to internal
                # error
                LOGGER.warning(
                    "Message was Nack'ed by broker: nack=%r; channel=%s; "
                    "exchange=%s; routing_key=%s; mandatory=%r; ",
                    conf_method, self.channel_number, exchange, routing_key,
                    mandatory)
                if self._puback_return is not None:
                    returned_messages: list[ReturnedMessage] = [
                        self._puback_return
                    ]
                    self._puback_return = None
                else:
                    returned_messages = []
                raise exceptions.NackError(returned_messages)

            assert isinstance(conf_method,
                              pika.spec.Basic.Ack), (conf_method)

            if self._puback_return is not None:
                # Unroutable message was returned
                messages = [self._puback_return]
                self._puback_return = None
                raise exceptions.UnroutableError(messages)
    else:
        # In non-publisher-acknowledgments mode
        self._impl.basic_publish(exchange=exchange,
                                 routing_key=routing_key,
                                 body=body,
                                 properties=properties,
                                 mandatory=mandatory)
        self._flush_output()

basic_qos

basic_qos(
    prefetch_size: int = 0,
    prefetch_count: int = 0,
    global_qos: bool = False,
) -> None

Specify quality of service.

This method requests a specific quality of service. The QoS can be specified for the current channel or for all channels on the connection. The client can request that messages be sent in advance so that when the client finishes processing a message, the following message is already held locally, rather than needing to be sent down the channel. Prefetching gives a performance improvement.

PARAMETER DESCRIPTION
prefetch_size

This field specifies the prefetch window size. The server will send a message in advance if it is equal to or smaller in size than the available prefetch size (and also falls into other prefetch limits). May be set to zero, meaning "no specific limit", although other prefetch limits may still apply. The prefetch-size is ignored if the no-ack option is set in the consumer.

TYPE: int DEFAULT: 0

prefetch_count

Specifies a prefetch window in terms of whole messages. This field may be used in combination with the prefetch-size field; a message will only be sent in advance if both prefetch windows (and those at the channel and connection level) allow it. The prefetch-count is ignored if the no-ack option is set in the consumer.

TYPE: int DEFAULT: 0

global_qos

Should the QoS apply to all channels on the connection.

TYPE: bool DEFAULT: False

Source code in pika/adapters/blocking_connection.py
def basic_qos(self,
              prefetch_size: int = 0,
              prefetch_count: int = 0,
              global_qos: bool = False) -> None:
    """
    Specify quality of service.

    This method requests a specific quality of service. The QoS can be specified for the current
    channel or for all channels on the connection. The client can request that messages be sent
    in advance so that when the client finishes processing a message, the following message is
    already held locally, rather than needing to be sent down the channel. Prefetching gives a
    performance improvement.

    :param prefetch_size: This field specifies the prefetch window size. The server will send a
        message in advance if it is equal to or smaller in size than the available prefetch size
        (and also falls into other prefetch limits). May be set to zero, meaning "no specific
        limit", although other prefetch limits may still apply. The prefetch-size is ignored if
        the no-ack option is set in the consumer.
    :param prefetch_count: Specifies a prefetch window in terms of whole messages. This field
        may be used in combination with the prefetch-size field; a message will only be sent in
        advance if both prefetch windows (and those at the channel and connection level) allow
        it. The prefetch-count is ignored if the no-ack option is set in the consumer.
    :param global_qos: Should the QoS apply to all channels on the connection.
    """
    with _CallbackResult() as qos_ok_result:
        self._impl.basic_qos(callback=qos_ok_result.signal_once,
                             prefetch_size=prefetch_size,
                             prefetch_count=prefetch_count,
                             global_qos=global_qos)
        self._flush_output(qos_ok_result.is_ready)

basic_recover

basic_recover(requeue: bool = False) -> None

This method asks the server to redeliver all unacknowledged messages on a specified channel. Zero or more messages may be redelivered. This method replaces the asynchronous Recover.

PARAMETER DESCRIPTION
requeue

If False, the message will be redelivered to the original recipient. If True, the server will attempt to requeue the message, potentially then delivering it to an alternative subscriber.

TYPE: bool DEFAULT: False

Source code in pika/adapters/blocking_connection.py
def basic_recover(self, requeue: bool = False) -> None:
    """
    This method asks the server to redeliver all unacknowledged messages on a specified channel.
    Zero or more messages may be redelivered. This method replaces the asynchronous Recover.

    :param requeue: If False, the message will be redelivered to the original recipient. If
        True, the server will attempt to requeue the message, potentially then delivering it to
        an alternative subscriber.
    """
    with _CallbackResult() as recover_ok_result:
        self._impl.basic_recover(requeue=requeue,
                                 callback=recover_ok_result.signal_once)
        self._flush_output(recover_ok_result.is_ready)

basic_reject

basic_reject(
    delivery_tag: int = 0, requeue: bool = True
) -> None

Reject an incoming message.

This method allows a client to reject a message. It can be used to interrupt and cancel large incoming messages, or return untreatable messages to their original queue.

PARAMETER DESCRIPTION
delivery_tag

The server-assigned delivery tag

TYPE: int DEFAULT: 0

requeue

If requeue is true, the server will attempt to requeue the message. If requeue is false or the requeue attempt fails the messages are discarded or dead- lettered.

TYPE: bool DEFAULT: True

Source code in pika/adapters/blocking_connection.py
def basic_reject(self, delivery_tag: int = 0, requeue: bool = True) -> None:
    """
    Reject an incoming message.

    This method allows a client to reject a message. It can be used to interrupt and cancel
    large incoming messages, or return untreatable messages to their original queue.

    :param delivery_tag: The server-assigned delivery tag
    :param requeue: If requeue is true, the server will attempt to requeue the message. If
        requeue is false or the requeue attempt fails the messages are discarded or dead-
        lettered.
    """
    self._impl.basic_reject(delivery_tag=delivery_tag, requeue=requeue)
    self._flush_output()

cancel

cancel() -> int

Cancel the queue consumer created by BlockingChannel.consume, rejecting all pending ackable messages.

NOTE: If you're looking to cancel a consumer issued with BlockingChannel.basic_consume then you should call BlockingChannel.basic_cancel.

RETURNS DESCRIPTION
int

The number of messages requeued by Basic.Nack. NEW in 0.10.0: returns 0

Source code in pika/adapters/blocking_connection.py
def cancel(self) -> int:
    """
    Cancel the queue consumer created by `BlockingChannel.consume`, rejecting all pending
    ackable messages.

    NOTE: If you're looking to cancel a consumer issued with
    BlockingChannel.basic_consume then you should call
    BlockingChannel.basic_cancel.

    :returns: The number of messages requeued by Basic.Nack.
        NEW in 0.10.0: returns 0
    """
    if self._queue_consumer_generator is None:
        LOGGER.warning('cancel: queue consumer generator is inactive '
                       '(already cancelled by client or broker?)')
        return 0

    try:
        _, auto_ack, _ = self._queue_consumer_generator.params
        if not auto_ack:
            # Reject messages held by queue consumer generator; NOTE: we
            # can't use basic_nack with the multiple option to avoid nacking
            # messages already held by our client.
            pending_events = self._queue_consumer_generator.pending_events
            # NOTE `get_waiting_message_count` adjusts for `Basic.Cancel`
            #      from the server at the end (if any)
            for _ in range(self.get_waiting_message_count()):
                evt = pending_events.popleft()
                self._impl.basic_reject(evt.method.delivery_tag,
                                        requeue=True)

        self.basic_cancel(self._queue_consumer_generator.consumer_tag)
    finally:
        self._queue_consumer_generator = None

    # Return 0 for compatibility with legacy implementation; the number of
    # nacked messages is not meaningful since only messages consumed with
    # auto_ack=False may be nacked, and those arriving after calling
    # basic_cancel will be rejected automatically by impl channel, so we'll
    # never know how many of those were nacked.
    return 0

close

close(
    reply_code: int = 0, reply_text: str = 'Normal shutdown'
) -> None

Will invoke a clean shutdown of the channel with the AMQP Broker.

PARAMETER DESCRIPTION
reply_code

The reply code to close the channel with

TYPE: int DEFAULT: 0

reply_text

The reply text to close the channel with

TYPE: str DEFAULT: 'Normal shutdown'

Source code in pika/adapters/blocking_connection.py
def close(self,
          reply_code: int = 0,
          reply_text: str = 'Normal shutdown') -> None:
    """
    Will invoke a clean shutdown of the channel with the AMQP Broker.

    :param reply_code: The reply code to close the channel with
    :param reply_text: The reply text to close the channel with
    """
    LOGGER.debug('Channel.close(%s, %s)', reply_code, reply_text)

    self._impl._raise_if_not_open()

    try:
        # Cancel remaining consumers
        self._cancel_all_consumers()

        # Close the channel
        self._impl.close(reply_code=reply_code, reply_text=reply_text)
        self._flush_output(lambda: self.is_closed)
    finally:
        self._cleanup()

confirm_delivery

confirm_delivery() -> None

Turn on RabbitMQ-proprietary Confirm mode in the channel.

For more information see: https://www.rabbitmq.com/confirms.html

Source code in pika/adapters/blocking_connection.py
def confirm_delivery(self) -> None:
    """
    Turn on RabbitMQ-proprietary Confirm mode in the channel.

    For more information see:
        https://www.rabbitmq.com/confirms.html
    """
    if self._delivery_confirmation:
        LOGGER.error(
            'confirm_delivery: confirmation was already enabled '
            'on channel=%s', self.channel_number)
        return

    with _CallbackResult() as select_ok_result:
        self._impl.confirm_delivery(
            ack_nack_callback=self._message_confirmation_result.
            set_value_once,
            callback=select_ok_result.signal_once)

        self._flush_output(select_ok_result.is_ready)

    self._delivery_confirmation = True

    # Unroutable messages returned after this point will be in the context
    # of publisher acknowledgments
    self._impl.add_on_return_callback(self._on_puback_message_returned)

consume

consume(
    queue: str,
    auto_ack: bool = False,
    exclusive: bool = False,
    arguments: dict[str, Any] | None = None,
    inactivity_timeout: float | None = None,
    consumer_tag: str | None = None,
) -> Generator[
    tuple[
        Deliver | None, BasicProperties | None, bytes | None
    ]
]

Blocking consumption of a queue instead of via a callback.

This method is a generator that yields each message as a tuple of method, properties, and body. The active generator iterator terminates when the consumer is cancelled by client via BlockingChannel.cancel() or by broker.

Example: :: for method, properties, body in channel.consume('queue'): print(body) channel.basic_ack(method.delivery_tag)

You should call BlockingChannel.cancel() when you escape out of the generator loop.

If you don't cancel this consumer, then next call on the same channel to consume() with the exact same (queue, auto_ack, exclusive) parameters will resume the existing consumer generator; however, calling with different parameters will result in an exception.

:yields: tuple(spec.Basic.Deliver, spec.BasicProperties, bytes)

PARAMETER DESCRIPTION
queue

The queue name to consume

TYPE: str

auto_ack

Tell the broker to not expect a ack/nack response

TYPE: bool DEFAULT: False

exclusive

Don't allow other consumers on the queue

TYPE: bool DEFAULT: False

arguments

Custom key/value pair arguments for the consumer

TYPE: dict[str, Any] | None DEFAULT: None

inactivity_timeout

if a number is given (in seconds), will cause the method to yield (None, None, None) after the given period of inactivity; this permits for pseudo-regular maintenance activities to be carried out by the user while waiting for messages to arrive. If None is given (default), then the method blocks until the next event arrives. NOTE that timing granularity is limited by the timer resolution of the underlying implementation. NEW in pika 0.10.0.

TYPE: float | None DEFAULT: None

consumer_tag

Specify your own consumer tag

TYPE: str | None DEFAULT: None

RAISES DESCRIPTION
ValueError

if consumer-creation parameters don't match those of the existing queue consumer generator, if any. NEW in pika 0.10.0

ChannelClosed

when this channel is closed by broker.

Source code in pika/adapters/blocking_connection.py
def consume(
    self,
    queue: str,
    auto_ack: bool = False,
    exclusive: bool = False,
    arguments: dict[str, Any] | None = None,
    inactivity_timeout: float | None = None,
    consumer_tag: str | None = None
) -> Generator[tuple[pika.spec.Basic.Deliver | None,
                     pika.spec.BasicProperties | None, bytes | None]]:
    """
    Blocking consumption of a queue instead of via a callback.

    This method is a generator that yields each message as a tuple of method,
    properties, and body. The active generator iterator terminates when the
    consumer is cancelled by client via `BlockingChannel.cancel()` or by
    broker.

    Example:
    ::
        for method, properties, body in channel.consume('queue'):
            print(body)
            channel.basic_ack(method.delivery_tag)

    You should call `BlockingChannel.cancel()` when you escape out of the
    generator loop.

    If you don't cancel this consumer, then next call on the same channel
    to `consume()` with the exact same (queue, auto_ack, exclusive) parameters
    will resume the existing consumer generator; however, calling with
    different parameters will result in an exception.

    :param queue: The queue name to consume
    :param auto_ack: Tell the broker to not expect a ack/nack response
    :param exclusive: Don't allow other consumers on the queue
    :param arguments: Custom key/value pair arguments for the consumer
    :param inactivity_timeout: if a number is given (in
        seconds), will cause the method to yield (None, None, None) after the
        given period of inactivity; this permits for pseudo-regular maintenance
        activities to be carried out by the user while waiting for messages
        to arrive. If None is given (default), then the method blocks until
        the next event arrives. NOTE that timing granularity is limited by
        the timer resolution of the underlying implementation.
        NEW in pika 0.10.0.
    :param consumer_tag: Specify your own consumer tag

    :yields: tuple(spec.Basic.Deliver, spec.BasicProperties, bytes)

    :raises ValueError: if consumer-creation parameters don't match those
        of the existing queue consumer generator, if any.
        NEW in pika 0.10.0
    :raises ChannelClosed: when this channel is closed by broker.
    """
    self._impl._raise_if_not_open()

    params = (queue, auto_ack, exclusive)

    if self._queue_consumer_generator is not None:
        if params != self._queue_consumer_generator.params:
            raise ValueError(
                'Consume with different params not allowed on existing '
                f'queue consumer generator; previous params: {self._queue_consumer_generator.params!r}; '
                f'new params: {(queue, auto_ack, exclusive)!r}')
    else:
        LOGGER.debug('Creating new queue consumer generator; params: %r',
                     params)
        # Need a consumer tag to register consumer info before sending
        # request to broker, because I/O might pick up incoming messages
        # in addition to Basic.Consume-ok
        if not consumer_tag:
            consumer_tag = self._impl._generate_consumer_tag()

        self._queue_consumer_generator = _QueueConsumerGeneratorInfo(
            params, consumer_tag)

        try:
            self._basic_consume_impl(
                queue=queue,
                auto_ack=auto_ack,
                exclusive=exclusive,
                consumer_tag=consumer_tag,
                arguments=arguments,
                alternate_event_sink=self._on_consumer_generator_event)
        except Exception:
            self._queue_consumer_generator = None
            raise

        LOGGER.info('Created new queue consumer generator %r',
                    self._queue_consumer_generator)

    while self._queue_consumer_generator is not None:
        # Process pending events
        if self._queue_consumer_generator.pending_events:
            evt = self._queue_consumer_generator.pending_events.popleft()
            if type(evt) is _ConsumerCancellationEvt:
                # Consumer was cancelled by broker
                self._queue_consumer_generator = None
                break
            else:
                yield (evt.method, evt.properties, evt.body)
                continue

        if inactivity_timeout is None:
            # Wait indefinitely for a message to arrive, while processing
            # I/O events and triggering ChannelClosed exception when the
            # channel fails
            self._process_data_events(time_limit=None)
            continue

        # Wait with inactivity timeout
        wait_start_time = pika._utils.time_now()
        wait_deadline = wait_start_time + inactivity_timeout
        delta = inactivity_timeout

        while (self._queue_consumer_generator is not None and
               not self._queue_consumer_generator.pending_events):

            self._process_data_events(time_limit=delta)

            if not self._queue_consumer_generator:
                # Consumer was cancelled by client
                break

            if self._queue_consumer_generator.pending_events:
                # Got message(s)
                break

            delta = wait_deadline - pika._utils.time_now()
            if delta <= 0.0:
                # Signal inactivity timeout
                yield (None, None, None)
                break

exchange_bind

exchange_bind(
    destination: str,
    source: str,
    routing_key: str = '',
    arguments: dict[str, Any] | None = None,
) -> Method[BindOk]

Bind an exchange to another exchange.

PARAMETER DESCRIPTION
destination

The destination exchange to bind

TYPE: str

source

The source exchange to bind to

TYPE: str

routing_key

The routing key to bind on

TYPE: str DEFAULT: ''

arguments

Custom key/value pair arguments for the binding

TYPE: dict[str, Any] | None DEFAULT: None

RETURNS DESCRIPTION
Method[BindOk]

Method frame from the Exchange.Bind-ok response

Source code in pika/adapters/blocking_connection.py
def exchange_bind(
    self,
    destination: str,
    source: str,
    routing_key: str = '',
    arguments: dict[str, Any] | None = None
) -> pika.frame.Method[pika.spec.Exchange.BindOk]:
    """
    Bind an exchange to another exchange.

    :param destination: The destination exchange to bind
    :param source: The source exchange to bind to
    :param routing_key: The routing key to bind on
    :param arguments: Custom key/value pair arguments for the binding
    :returns: Method frame from the Exchange.Bind-ok response
    """
    validators.require_string(destination, 'destination')
    validators.require_string(source, 'source')
    with _CallbackResult(
            self._MethodFrameCallbackResultArgs) as bind_ok_result:
        self._impl.exchange_bind(destination=destination,
                                 source=source,
                                 routing_key=routing_key,
                                 arguments=arguments,
                                 callback=bind_ok_result.set_value_once)

        self._flush_output(bind_ok_result.is_ready)
        return bind_ok_result.value.method_frame

exchange_declare

exchange_declare(
    exchange: str,
    exchange_type: ExchangeType | str = direct,
    passive: bool = False,
    durable: bool = False,
    auto_delete: bool = False,
    internal: bool = False,
    arguments: dict[str, Any] | None = None,
) -> None

This method creates an exchange if it does not already exist, and if the exchange exists, verifies that it is of the correct and expected class.

If passive set, the server will reply with Declare-Ok if the exchange already exists with the same name, and raise an error if not and if the exchange does not already exist, the server MUST raise a channel exception with reply code 404 (not found).

PARAMETER DESCRIPTION
exchange

The exchange name consists of a non-empty sequence of these characters: letters, digits, hyphen, underscore, period, or colon.

TYPE: str

exchange_type

The exchange type to use

TYPE: ExchangeType | str DEFAULT: direct

passive

Perform a declare or just check to see if it exists

TYPE: bool DEFAULT: False

durable

Survive a reboot of RabbitMQ

TYPE: bool DEFAULT: False

auto_delete

Remove when no more queues are bound to it

TYPE: bool DEFAULT: False

internal

Can only be published to by other exchanges

TYPE: bool DEFAULT: False

arguments

Custom key/value pair arguments for the exchange

TYPE: dict[str, Any] | None DEFAULT: None

RETURNS DESCRIPTION
None

Method frame from the Exchange.Declare-ok response

Source code in pika/adapters/blocking_connection.py
def exchange_declare(self,
                     exchange: str,
                     exchange_type: (ExchangeType |
                                     str) = ExchangeType.direct,
                     passive: bool = False,
                     durable: bool = False,
                     auto_delete: bool = False,
                     internal: bool = False,
                     arguments: dict[str, Any] | None = None) -> None:
    """
    This method creates an exchange if it does not already exist, and if the exchange exists,
    verifies that it is of the correct and expected class.

    If passive set, the server will reply with Declare-Ok if the exchange
    already exists with the same name, and raise an error if not and if the
    exchange does not already exist, the server MUST raise a channel
    exception with reply code 404 (not found).

    :param exchange: The exchange name consists of a non-empty sequence of
                      these characters: letters, digits, hyphen, underscore,
                      period, or colon.
    :param exchange_type: The exchange type to use
    :param passive: Perform a declare or just check to see if it exists
    :param durable: Survive a reboot of RabbitMQ
    :param auto_delete: Remove when no more queues are bound to it
    :param internal: Can only be published to by other exchanges
    :param arguments: Custom key/value pair arguments for the exchange
    :returns: Method frame from the Exchange.Declare-ok response
    """
    validators.require_string(exchange, 'exchange')
    with _CallbackResult(
            self._MethodFrameCallbackResultArgs) as declare_ok_result:
        self._impl.exchange_declare(
            exchange=exchange,
            exchange_type=exchange_type,
            passive=passive,
            durable=durable,
            auto_delete=auto_delete,
            internal=internal,
            arguments=arguments,
            callback=declare_ok_result.set_value_once)

        self._flush_output(declare_ok_result.is_ready)
        return declare_ok_result.value.method_frame

exchange_delete

exchange_delete(
    exchange: str | None = None, if_unused: bool = False
) -> Method[DeleteOk]

Delete the exchange.

PARAMETER DESCRIPTION
exchange

The exchange name

TYPE: str | None DEFAULT: None

if_unused

only delete if the exchange is unused

TYPE: bool DEFAULT: False

RETURNS DESCRIPTION
Method[DeleteOk]

Method frame from the Exchange.Delete-ok response

Source code in pika/adapters/blocking_connection.py
def exchange_delete(self,
                    exchange: str | None = None,
                    if_unused: bool = False
                   ) -> pika.frame.Method[pika.spec.Exchange.DeleteOk]:
    """
    Delete the exchange.

    :param exchange: The exchange name
    :param if_unused: only delete if the exchange is unused
    :returns: Method frame from the Exchange.Delete-ok response
    """
    with _CallbackResult(
            self._MethodFrameCallbackResultArgs) as delete_ok_result:
        self._impl.exchange_delete(exchange=exchange,
                                   if_unused=if_unused,
                                   callback=delete_ok_result.set_value_once)

        self._flush_output(delete_ok_result.is_ready)
        return delete_ok_result.value.method_frame

exchange_unbind

exchange_unbind(
    destination: str | None = None,
    source: str | None = None,
    routing_key: str = '',
    arguments: dict[str, Any] | None = None,
) -> Method[UnbindOk]

Unbind an exchange from another exchange.

PARAMETER DESCRIPTION
destination

The destination exchange to unbind

TYPE: str | None DEFAULT: None

source

The source exchange to unbind from

TYPE: str | None DEFAULT: None

routing_key

The routing key to unbind

TYPE: str DEFAULT: ''

arguments

Custom key/value pair arguments for the binding

TYPE: dict[str, Any] | None DEFAULT: None

RETURNS DESCRIPTION
Method[UnbindOk]

Method frame from the Exchange.Unbind-ok response

Source code in pika/adapters/blocking_connection.py
def exchange_unbind(
    self,
    destination: str | None = None,
    source: str | None = None,
    routing_key: str = '',
    arguments: dict[str, Any] | None = None
) -> pika.frame.Method[pika.spec.Exchange.UnbindOk]:
    """
    Unbind an exchange from another exchange.

    :param destination: The destination exchange to unbind
    :param source: The source exchange to unbind from
    :param routing_key: The routing key to unbind
    :param arguments: Custom key/value pair arguments for the binding
    :returns: Method frame from the Exchange.Unbind-ok response
    """
    with _CallbackResult(
            self._MethodFrameCallbackResultArgs) as unbind_ok_result:
        self._impl.exchange_unbind(destination=destination,
                                   source=source,
                                   routing_key=routing_key,
                                   arguments=arguments,
                                   callback=unbind_ok_result.set_value_once)

        self._flush_output(unbind_ok_result.is_ready)
        return unbind_ok_result.value.method_frame

flow

flow(active: bool) -> bool

Turn Channel flow control off and on.

NOTE: RabbitMQ doesn't support active=False; per https://www.rabbitmq.com/specification.html: "active=false is not supported by the server. Limiting prefetch with basic.qos provides much better control"

For more information, please reference:

https://www.rabbitmq.com/amqp-0-9-1-reference.html#channel.flow

PARAMETER DESCRIPTION
active

Turn flow on (True) or off (False)

TYPE: bool

RETURNS DESCRIPTION
bool

True if broker will start or continue sending; False if not

Source code in pika/adapters/blocking_connection.py
def flow(self, active: bool) -> bool:
    """
    Turn Channel flow control off and on.

    NOTE: RabbitMQ doesn't support active=False; per
    https://www.rabbitmq.com/specification.html: "active=false is not
    supported by the server. Limiting prefetch with basic.qos provides much
    better control"

    For more information, please reference:

    https://www.rabbitmq.com/amqp-0-9-1-reference.html#channel.flow

    :param active: Turn flow on (True) or off (False)
    :returns: True if broker will start or continue sending; False if not
    """
    with _CallbackResult(self._FlowOkCallbackResultArgs) as flow_ok_result:
        self._impl.flow(active=active,
                        callback=flow_ok_result.set_value_once)
        self._flush_output(flow_ok_result.is_ready)
        return flow_ok_result.value.active

get_waiting_message_count

get_waiting_message_count() -> int

Returns the number of messages that may be retrieved from the current queue consumer generator via BlockingChannel.consume without blocking. NEW in pika 0.10.0.

RETURNS DESCRIPTION
int

The number of waiting messages

Source code in pika/adapters/blocking_connection.py
def get_waiting_message_count(self) -> int:
    """
    Returns the number of messages that may be retrieved from the current queue consumer
    generator via `BlockingChannel.consume` without blocking. NEW in pika 0.10.0.

    :returns: The number of waiting messages
    """
    if self._queue_consumer_generator is not None:
        pending_events = self._queue_consumer_generator.pending_events
        count = len(pending_events)
        if count and type(pending_events[-1]) is _ConsumerCancellationEvt:
            count -= 1
    else:
        count = 0

    return count

queue_bind

queue_bind(
    queue: str,
    exchange: str,
    routing_key: str | None = None,
    arguments: dict[str, Any] | None = None,
) -> Method[BindOk]

Bind the queue to the specified exchange.

PARAMETER DESCRIPTION
queue

The queue to bind to the exchange

TYPE: str

exchange

The source exchange to bind to

TYPE: str

routing_key

The routing key to bind on

TYPE: str | None DEFAULT: None

arguments

Custom key/value pair arguments for the binding

TYPE: dict[str, Any] | None DEFAULT: None

RETURNS DESCRIPTION
Method[BindOk]

Method frame from the Queue.Bind-ok response

Source code in pika/adapters/blocking_connection.py
def queue_bind(
    self,
    queue: str,
    exchange: str,
    routing_key: str | None = None,
    arguments: dict[str, Any] | None = None
) -> pika.frame.Method[pika.spec.Queue.BindOk]:
    """
    Bind the queue to the specified exchange.

    :param queue: The queue to bind to the exchange
    :param exchange: The source exchange to bind to
    :param routing_key: The routing key to bind on
    :param arguments: Custom key/value pair arguments for the binding
    :returns: Method frame from the Queue.Bind-ok response
    """
    validators.require_string(queue, 'queue')
    validators.require_string(exchange, 'exchange')
    with _CallbackResult(
            self._MethodFrameCallbackResultArgs) as bind_ok_result:
        self._impl.queue_bind(queue=queue,
                              exchange=exchange,
                              routing_key=routing_key,
                              arguments=arguments,
                              callback=bind_ok_result.set_value_once)
        self._flush_output(bind_ok_result.is_ready)
        return bind_ok_result.value.method_frame

queue_declare

queue_declare(
    queue: str,
    passive: bool = False,
    durable: bool = False,
    exclusive: bool = False,
    auto_delete: bool = False,
    arguments: dict[str, Any] | None = None,
) -> Method[DeclareOk]

Declare queue, create if needed.

This method creates or checks a queue. When creating a new queue the client can specify various properties that control the durability of the queue and its contents, and the level of sharing for the queue.

Use an empty string as the queue name for the broker to auto-generate one. Retrieve this auto-generated queue name from the returned spec.Queue.DeclareOk method frame.

PARAMETER DESCRIPTION
queue

The queue name; if empty string, the broker will create a unique queue name

TYPE: str

passive

Only check to see if the queue exists and raise ChannelClosed if it doesn't

TYPE: bool DEFAULT: False

durable

Survive reboots of the broker

TYPE: bool DEFAULT: False

exclusive

Only allow access by the current connection

TYPE: bool DEFAULT: False

auto_delete

Delete after consumer cancels or disconnects

TYPE: bool DEFAULT: False

arguments

Custom key/value arguments for the queue

TYPE: dict[str, Any] | None DEFAULT: None

RETURNS DESCRIPTION
Method[DeclareOk]

Method frame from the Queue.Declare-ok response

Source code in pika/adapters/blocking_connection.py
def queue_declare(
    self,
    queue: str,
    passive: bool = False,
    durable: bool = False,
    exclusive: bool = False,
    auto_delete: bool = False,
    arguments: dict[str, Any] | None = None
) -> pika.frame.Method[pika.spec.Queue.DeclareOk]:
    """
    Declare queue, create if needed.

    This method creates or checks a queue. When creating a new queue the client can specify various
    properties that control the durability of the queue and its contents,
    and the level of sharing for the queue.

    Use an empty string as the queue name for the broker to auto-generate
    one. Retrieve this auto-generated queue name from the returned
    `spec.Queue.DeclareOk` method frame.

    :param queue: The queue name; if empty string, the broker will
        create a unique queue name
    :param passive: Only check to see if the queue exists and raise
      `ChannelClosed` if it doesn't
    :param durable: Survive reboots of the broker
    :param exclusive: Only allow access by the current connection
    :param auto_delete: Delete after consumer cancels or disconnects
    :param arguments: Custom key/value arguments for the queue
    :returns: Method frame from the Queue.Declare-ok response
    """
    validators.require_string(queue, 'queue')
    with _CallbackResult(
            self._MethodFrameCallbackResultArgs) as declare_ok_result:
        self._impl.queue_declare(queue=queue,
                                 passive=passive,
                                 durable=durable,
                                 exclusive=exclusive,
                                 auto_delete=auto_delete,
                                 arguments=arguments,
                                 callback=declare_ok_result.set_value_once)

        self._flush_output(declare_ok_result.is_ready)
        return declare_ok_result.value.method_frame

queue_delete

queue_delete(
    queue: str,
    if_unused: bool = False,
    if_empty: bool = False,
) -> Method[DeleteOk]

Delete a queue from the broker.

PARAMETER DESCRIPTION
queue

The queue to delete

TYPE: str

if_unused

only delete if it's unused

TYPE: bool DEFAULT: False

if_empty

only delete if the queue is empty

TYPE: bool DEFAULT: False

RETURNS DESCRIPTION
Method[DeleteOk]

Method frame from the Queue.Delete-ok response

Source code in pika/adapters/blocking_connection.py
def queue_delete(self,
                 queue: str,
                 if_unused: bool = False,
                 if_empty: bool = False
                ) -> pika.frame.Method[pika.spec.Queue.DeleteOk]:
    """
    Delete a queue from the broker.

    :param queue: The queue to delete
    :param if_unused: only delete if it's unused
    :param if_empty: only delete if the queue is empty
    :returns: Method frame from the Queue.Delete-ok response
    """
    with _CallbackResult(
            self._MethodFrameCallbackResultArgs) as delete_ok_result:
        self._impl.queue_delete(queue=queue,
                                if_unused=if_unused,
                                if_empty=if_empty,
                                callback=delete_ok_result.set_value_once)

        self._flush_output(delete_ok_result.is_ready)
        return delete_ok_result.value.method_frame

queue_purge

queue_purge(queue: str) -> Method[PurgeOk]

Purge all of the messages from the specified queue.

PARAMETER DESCRIPTION
queue

The queue to purge

TYPE: str

RETURNS DESCRIPTION
Method[PurgeOk]

Method frame from the Queue.Purge-ok response

Source code in pika/adapters/blocking_connection.py
def queue_purge(self,
                queue: str) -> pika.frame.Method[pika.spec.Queue.PurgeOk]:
    """
    Purge all of the messages from the specified queue.

    :param queue: The queue to purge
    :returns: Method frame from the Queue.Purge-ok response
    """
    with _CallbackResult(
            self._MethodFrameCallbackResultArgs) as purge_ok_result:
        self._impl.queue_purge(queue=queue,
                               callback=purge_ok_result.set_value_once)
        self._flush_output(purge_ok_result.is_ready)
        return purge_ok_result.value.method_frame

queue_unbind

queue_unbind(
    queue: str,
    exchange: str,
    routing_key: str | None = None,
    arguments: dict[str, Any] | None = None,
) -> Method[UnbindOk]

Unbind a queue from an exchange.

PARAMETER DESCRIPTION
queue

The queue to unbind from the exchange

TYPE: str

exchange

The source exchange to bind from

TYPE: str

routing_key

The routing key to unbind

TYPE: str | None DEFAULT: None

arguments

Custom key/value pair arguments for the binding

TYPE: dict[str, Any] | None DEFAULT: None

RETURNS DESCRIPTION
Method[UnbindOk]

Method frame from the Queue.Unbind-ok response

Source code in pika/adapters/blocking_connection.py
def queue_unbind(
    self,
    queue: str,
    exchange: str,
    routing_key: str | None = None,
    arguments: dict[str, Any] | None = None
) -> pika.frame.Method[pika.spec.Queue.UnbindOk]:
    """
    Unbind a queue from an exchange.

    :param queue: The queue to unbind from the exchange
    :param exchange: The source exchange to bind from
    :param routing_key: The routing key to unbind
    :param arguments: Custom key/value pair arguments for the binding
    :returns: Method frame from the Queue.Unbind-ok response
    """
    with _CallbackResult(
            self._MethodFrameCallbackResultArgs) as unbind_ok_result:
        self._impl.queue_unbind(queue=queue,
                                exchange=exchange,
                                routing_key=routing_key,
                                arguments=arguments,
                                callback=unbind_ok_result.set_value_once)
        self._flush_output(unbind_ok_result.is_ready)
        return unbind_ok_result.value.method_frame

start_consuming

start_consuming() -> None

Processes I/O events and dispatches timers and basic_consume callbacks until all consumers are cancelled.

NOTE: this blocking function may not be called from the scope of a pika callback, because dispatching basic_consume callbacks from this context would constitute recursion.

RAISES DESCRIPTION
pika.exceptions.ReentrancyError

if called from the scope of a BlockingConnection or BlockingChannel callback

ChannelClosed

when this channel is closed by broker.

Source code in pika/adapters/blocking_connection.py
def start_consuming(self) -> None:
    """
    Processes I/O events and dispatches timers and `basic_consume` callbacks until all consumers
    are cancelled.

    NOTE: this blocking function may not be called from the scope of a
    pika callback, because dispatching `basic_consume` callbacks from this
    context would constitute recursion.

    :raises pika.exceptions.ReentrancyError: if called from the scope of a
        `BlockingConnection` or `BlockingChannel` callback
    :raises ChannelClosed: when this channel is closed by broker.
    """
    # Check if called from the scope of an event dispatch callback
    with self.connection._acquire_event_dispatch() as dispatch_allowed:
        if not dispatch_allowed:
            raise exceptions.ReentrancyError(
                'start_consuming may not be called from the scope of '
                'another BlockingConnection or BlockingChannel callback')

    self._impl._raise_if_not_open()

    # Process events as long as consumers exist on this channel
    while self._consumer_infos:
        # This will raise ChannelClosed if channel is closed by broker
        self._process_data_events(time_limit=None)

stop_consuming

stop_consuming(consumer_tag: str | None = None) -> None

Cancels all consumers, signalling the start_consuming loop to exit.

NOTE: pending non-ackable messages will be lost; pending ackable messages will be rejected.

PARAMETER DESCRIPTION
consumer_tag

if specified, only the consumer with the given tag will be cancelled

TYPE: str | None DEFAULT: None

Source code in pika/adapters/blocking_connection.py
def stop_consuming(self, consumer_tag: str | None = None) -> None:
    """
    Cancels all consumers, signalling the `start_consuming` loop to exit.

    NOTE: pending non-ackable messages will be lost; pending ackable
    messages will be rejected.

    :param consumer_tag: if specified, only the consumer with the given tag will be cancelled
    """
    if consumer_tag:
        self.basic_cancel(consumer_tag)
    else:
        self._cancel_all_consumers()

tx_commit

tx_commit() -> Method[CommitOk]

Commit a transaction.

RETURNS DESCRIPTION
Method[CommitOk]

Method frame from the Tx.Commit-ok response

Source code in pika/adapters/blocking_connection.py
def tx_commit(self) -> pika.frame.Method[pika.spec.Tx.CommitOk]:
    """
    Commit a transaction.

    :returns: Method frame from the Tx.Commit-ok response
    """
    with _CallbackResult(
            self._MethodFrameCallbackResultArgs) as commit_ok_result:
        self._impl.tx_commit(commit_ok_result.set_value_once)

        self._flush_output(commit_ok_result.is_ready)
        return commit_ok_result.value.method_frame

tx_rollback

tx_rollback() -> Method[RollbackOk]

Rollback a transaction.

RETURNS DESCRIPTION
Method[RollbackOk]

Method frame from the Tx.Rollback-ok response

Source code in pika/adapters/blocking_connection.py
def tx_rollback(self) -> pika.frame.Method[pika.spec.Tx.RollbackOk]:
    """
    Rollback a transaction.

    :returns: Method frame from the Tx.Rollback-ok response
    """
    with _CallbackResult(
            self._MethodFrameCallbackResultArgs) as rollback_ok_result:
        self._impl.tx_rollback(rollback_ok_result.set_value_once)

        self._flush_output(rollback_ok_result.is_ready)
        return rollback_ok_result.value.method_frame

tx_select

tx_select() -> Method[SelectOk]

Select standard transaction mode.

This method sets the channel to use standard transactions. The client must use this method at least once on a channel before using the Commit or Rollback methods.

RETURNS DESCRIPTION
Method[SelectOk]

Method frame from the Tx.Select-ok response

Source code in pika/adapters/blocking_connection.py
def tx_select(self) -> pika.frame.Method[pika.spec.Tx.SelectOk]:
    """
    Select standard transaction mode.

    This method sets the channel to use standard transactions. The client must use this method
    at least once on a channel before using the Commit or Rollback methods.

    :returns: Method frame from the Tx.Select-ok response
    """
    with _CallbackResult(
            self._MethodFrameCallbackResultArgs) as select_ok_result:
        self._impl.tx_select(select_ok_result.set_value_once)

        self._flush_output(select_ok_result.is_ready)
        return select_ok_result.value.method_frame