当前位置: 首页>>代码示例>>Python>>正文


Python SelectConnection.add_on_close_callback方法代码示例

本文整理汇总了Python中pika.adapters.SelectConnection.add_on_close_callback方法的典型用法代码示例。如果您正苦于以下问题:Python SelectConnection.add_on_close_callback方法的具体用法?Python SelectConnection.add_on_close_callback怎么用?Python SelectConnection.add_on_close_callback使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在pika.adapters.SelectConnection的用法示例。


在下文中一共展示了SelectConnection.add_on_close_callback方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: start

# 需要导入模块: from pika.adapters import SelectConnection [as 别名]
# 或者: from pika.adapters.SelectConnection import add_on_close_callback [as 别名]
 def start(self):
     # parameters require for the AMQP connection: user name and password...
     credentials = PlainCredentials(RabbitMQConfiguration().getRabbitMQProperty("gameLogicServerUserName"),
                                    RabbitMQConfiguration().getRabbitMQProperty("gameLogicServerUserName"))
     parameters = pika.ConnectionParameters(host=RabbitMQConfiguration().getRabbitMQProperty("gameLogicServerBrokerHost"),
                                            virtual_host=self.state.vhost,
                                            credentials=credentials)
     # instantiate a connection
     connection = SelectConnection(parameters=parameters,
                                   on_open_callback=self.on_connected)
     # required behavior on close
     connection.add_on_close_callback(self.on_close)
     # start the connection
     connection.ioloop.start()
开发者ID:Pandoro,项目名称:ZebroGaMQ,代码行数:16,代码来源:gamelogicchannel.py

示例2: Amqp

# 需要导入模块: from pika.adapters import SelectConnection [as 别名]
# 或者: from pika.adapters.SelectConnection import add_on_close_callback [as 别名]
class Amqp(object):
    def __init__(self, conf):

        # RabbitMQ general options
        self.cacertfile = conf['cacertfile']
        self.certfile = conf['certfile']
        self.exchange = conf['exchange']
        self.status_exchange = conf['status_exchange']
        self.fail_if_no_peer_cert = conf['fail_if_no_peer_cert']
        self.heartbeat = conf['heartbeat']
        self.host = conf['host']
        self.keyfile = conf['keyfile']
        self.password = conf['password']
        self.port = conf['port']
        self.ssl_port = conf['ssl_port']
        self.queue = conf['uuid']
        self.retry_timeout = conf['retry_timeout']
        self.ssl_auth = conf['ssl_auth']
        self.use_ssl = conf['use_ssl']
        self.username = conf['username']
        self.vhost = conf['vhost']

        # Connection and channel initialization
        self.connection = None
        self.channel = None

        # Plain credentials
        credentials = PlainCredentials(self.username, self.password)
        pika_options = {'host': self.host,
                        'port': self.port,
                        'virtual_host': self.vhost,
                        'credentials': credentials}

        # SSL options
        if self.use_ssl:
            pika_options['ssl'] = True
            pika_options['port'] = self.ssl_port
            if self.ssl_auth:
                pika_options['credentials'] = ExternalCredentials()
                pika_options['ssl_options'] = {
                    'ca_certs': self.cacertfile,
                    'certfile': self.certfile,
                    'keyfile': self.keyfile,
                    'cert_reqs': CERT_REQUIRED
                }

        if self.heartbeat:
            pika_options['heartbeat'] = self.heartbeat

        self.parameters = None

        try:
            self.parameters = pika.ConnectionParameters(**pika_options)
        except TypeError as err:
            self.logger.debug(err)
            # Let's be compatible with original pika version (no integer for
            # heartbeats and no ssl.
            self.logger.warning("Wrong pika lib version, won't use ssl.")
            pika_options['heartbeat'] = True
            if self.use_ssl:
                self.use_ssl = False
                pika_options['port'] = self.port
                del pika_options['ssl']
                if self.ssl_auth:
                    self.ssl_auth = False
                    del pika_options['ssl_options']

            self.parameters = pika.ConnectionParameters(**pika_options)

    def connect(self):
        SelectPoller.TIMEOUT = .1
        self.connection = SelectConnection(self.parameters, self.on_connected)
        self.connection.ioloop.start()

    def close(self, amqperror=False):
        if (self.connection and not self.connection.closing
            and not self.connection.closed):

            self.logger.debug("Closing connection")
            self.connection.close()
            #self.connection.ioloop.start()

    def on_remote_close(self, code, text):
        self.logger.debug("Remote channel close, code %d" % code)
        time.sleep(2)
        if code != 200:
            self.close()
            raise AmqpError(text)

    def on_connection_closed(self, frame):
        self.connection.ioloop.stop()

    def on_connected(self, connection):
        self.connection = connection
        self.connection.add_on_close_callback(self.on_connection_closed)
        self.connection.channel(self.on_channel_open)
开发者ID:Smartaleck,项目名称:synapse-agent,代码行数:98,代码来源:amqp.py


注:本文中的pika.adapters.SelectConnection.add_on_close_callback方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。