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
提供的函数、方法解析:
- add_on_close_callback
关闭时候的回调, callback定义 callback(pika.connection.Connection,exception)
- add_on_connection_blocked_callback
阻塞时候回调 回调定义: callback(connection, pika.frame.Method)
-
add_on_connection_unblocked_callback
解除阻塞时候回调
-
add_on_open_callback
打开时回调
-
add_on_open_error_callback
打开异常时候回调
- basic_nack
- channel(channel_number = None*,*on_open_callback = None )
建立通道
channel_number(int) - 要使用的通道编号,默认为下一个可用通道编号。
on_open_callback(callable)
- close
- consumer_cancel_notify
定服务器是否支持活动连接上的使用者取消通知 返回bool值
- create_connection(connection_configs, on_done, custom_ioloop=None, workflow=None)
建立连接
-
exchange_exchange_bindings
返回值bool 是否支持交换机修改
-
ioloop
-
is_closed
-
is_closing
-
is_open
-
publisher_confirms
是否支持发布确认
Tornado Connection Adapter
方法可参考基类BaseConnection
略
Twisted Connection Adapter
方法可参考基类BaseConnection
略