其他分享
首页 > 其他分享> > pika详解(三)SelectConnection及其他Connection

pika详解(三)SelectConnection及其他Connection

作者:互联网

pika详解(三)SelectConnection及其他Connection

   

pika除了block connection外还提供了其他非阻塞式的连接方式

SelectConnection

定义如下:

class SelectConnection(BaseConnection):
    def __init__(
            self,  # pylint: disable=R0913
            parameters=None,
            on_open_callback=None,
            on_open_error_callback=None,
            on_close_callback=None,
            custom_ioloop=None,
            internal_connection_workflow=True):
        """Create a new instance of the Connection object.

        :param pika.connection.Parameters parameters: Connection parameters
        :param callable on_open_callback: Method to call on connection open
        :param None | method on_open_error_callback: Called if the connection
            can't be established or connection establishment is interrupted by
            `Connection.close()`: on_open_error_callback(Connection, exception).
        :param None | method on_close_callback: Called when a previously fully
            open connection is closed:
            `on_close_callback(Connection, exception)`, where `exception` is
            either an instance of `exceptions.ConnectionClosed` if closed by
            user or broker or exception of another type that describes the cause
            of connection failure.
        :param None | IOLoop | nbio_interface.AbstractIOServices custom_ioloop:
            Provide a custom I/O Loop object.
        :param bool internal_connection_workflow: True for autonomous connection
            establishment which is default; False for externally-managed
            connection workflow via the `create_connection()` factory.
        :raises: RuntimeError

        """
        if isinstance(custom_ioloop, nbio_interface.AbstractIOServices):
            nbio = custom_ioloop
        else:
            nbio = SelectorIOServicesAdapter(custom_ioloop or IOLoop())

        super(SelectConnection, self).__init__(
            parameters,
            on_open_callback,
            on_open_error_callback,
            on_close_callback,
            nbio,
            internal_connection_workflow=internal_connection_workflow)

    @classmethod
    def create_connection(cls,
                          connection_configs,
                          on_done,
                          custom_ioloop=None,
                          workflow=None):
        """Implement
        :py:classmethod:`pika.adapters.BaseConnection.create_connection()`.

        """
        nbio = SelectorIOServicesAdapter(custom_ioloop or IOLoop())

        def connection_factory(params):
            """Connection factory."""
            if params is None:
                raise ValueError('Expected pika.connection.Parameters '
                                 'instance, but got None in params arg.')
            return cls(
                parameters=params,
                custom_ioloop=nbio,
                internal_connection_workflow=False)

        return cls._start_connection_workflow(
            connection_configs=connection_configs,
            connection_factory=connection_factory,
            nbio=nbio,
            workflow=workflow,
            on_done=on_done)
 

SelectConnection继承BaseConnection,只是重写了create_connection

在create_connection 中可以看到:

nbio = SelectorIOServicesAdapter(custom_ioloop or IOLoop())

说明这个类实现的 select IO

提供的函数、方法解析:

  1. add_on_close_callback

关闭时候的回调, callback定义 callback(pika.connection.Connection,exception)

  1. add_on_connection_blocked_callback

阻塞时候回调 回调定义: callback(connection, pika.frame.Method)

  1. add_on_connection_unblocked_callback

    解除阻塞时候回调

  2. add_on_open_callback

    打开时回调

  3. add_on_open_error_callback

打开异常时候回调

  1. basic_nack
  2. channel(channel_number = None*,*on_open_callback = None

建立通道

channel_number(int) - 要使用的通道编号,默认为下一个可用通道编号。
on_open_callback(callable)

  1. close
  2. consumer_cancel_notify

定服务器是否支持活动连接上的使用者取消通知 返回bool值

  1. create_connection(connection_configs, on_done, custom_ioloop=None, workflow=None)

建立连接

  1. exchange_exchange_bindings

    返回值bool 是否支持交换机修改

  2. ioloop

  3. is_closed

  4. is_closing

  5. is_open

  6. publisher_confirms

    是否支持发布确认

Tornado Connection Adapter

方法可参考基类BaseConnection

Twisted Connection Adapter

方法可参考基类BaseConnection

 

标签:SelectConnection,ioloop,None,pika,workflow,Connection,callback,connection,open
来源: https://www.cnblogs.com/xiao-xue-di/p/13856708.html