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


Python SelectConnection.close方法代码示例

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


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

示例1: BaseAMQPConnection

# 需要导入模块: from pika.adapters import SelectConnection [as 别名]
# 或者: from pika.adapters.SelectConnection import close [as 别名]
class BaseAMQPConnection(BaseConnection):
    """ An object which does an actual job of (re-)connecting to the the AMQP broker.
    Concrete subclasses implement either listening or publishing features.
    """
    def __init__(self, conn_params, item_name, properties=None):
        super(BaseAMQPConnection, self).__init__()
        self.conn_params = conn_params
        self.item_name = item_name
        self.properties = properties
        self.conn = None
        self.channel = None
        self.reconnect_exceptions = (TypeError, EnvironmentError)

    def _start(self):
        self.conn = SelectConnection(self.conn_params, self._on_connected)
        self.conn.ioloop.start()
        
    def _close(self):
        """ Actually close the connection.
        """
        if self.conn:
            try:
                self.conn.close()
            except socket.error, e:
                if e.errno != errno.EBADF:
                    # EBADF meant we actually had a connection but it was unusable (like the credentials were incorrect)
                    raise
开发者ID:azazel75,项目名称:zato,代码行数:29,代码来源:__init__.py

示例2: connect

# 需要导入模块: from pika.adapters import SelectConnection [as 别名]
# 或者: from pika.adapters.SelectConnection import close [as 别名]
    def connect(self, forever=False):
        name = self.name
        while True:
            try:
                connection = SelectConnection(
                    self.parameters, self.on_connected)
                log.debug('%s connected', name)
            except Exception:
                if not forever:
                    raise
                log.warning('%s cannot connect', name, exc_info=True)
                time.sleep(10)
                continue

            try:
                connection.ioloop.start()
            finally:
                try:
                    connection.close()
                    connection.ioloop.start() # allow connection to close
                except Exception:
                    pass

            if not forever:
                break
开发者ID:gswyhq,项目名称:hello-world,代码行数:27,代码来源:RabbitMQ发布者保持连接.py

示例3: testnb

# 需要导入模块: from pika.adapters import SelectConnection [as 别名]
# 或者: from pika.adapters.SelectConnection import close [as 别名]
def testnb():
    node = NodeNB()
    #ch = node.channel(('amq.direct', 'foo'), TestServer)
    ch = node.channel(TestServer)
    #ch = node.channel(TestClient)
    conn_parameters = ConnectionParameters()
    connection = SelectConnection(conn_parameters , node.on_connection_open)
    # Loop until CTRL-C
    try:
        # Start our blocking loop
        connection.ioloop.start()

    except KeyboardInterrupt:

        # Close the connection
        connection.close()

        # Loop until the connection is closed
        connection.ioloop.start()
开发者ID:sunetos,项目名称:anode,代码行数:21,代码来源:messaging.py

示例4: simple_server

# 需要导入模块: from pika.adapters import SelectConnection [as 别名]
# 或者: from pika.adapters.SelectConnection import close [as 别名]
def simple_server(ca_certs, keyfile, certfile, host_name, use_ssl):
    host = host_name
    if use_ssl:
        ssl_opts = {'ca_certs': ca_certs,
                    'keyfile': keyfile,
                    'certfile': certfile,
                    'cert_reqs': ssl.CERT_REQUIRED}
        port = 5671
    else:
        ssl_opts = None
        port = 5672
    parameters = ConnectionParameters(host, port,
        ssl=use_ssl, ssl_options=ssl_opts)
    connection = SelectConnection(parameters, on_connected)

    # Loop until CTRL-C
    try:
        # Start our blocking loop
        connection.ioloop.start()
    except KeyboardInterrupt:
        # Close the connection
        connection.close()
        # Loop until the connection is closed
        connection.ioloop.start()
开发者ID:JustOnce,项目名称:BlogSamples,代码行数:26,代码来源:server.py

示例5: SelectConnection

# 需要导入模块: from pika.adapters import SelectConnection [as 别名]
# 或者: from pika.adapters.SelectConnection import close [as 别名]
                              routing_key="krqueue",
                              body=jsonmsg,
                              properties=properties)

        print "demo_send:" + jsonmsg

    # Close our connection
    print "Closing client"
    connection.close()


if __name__ == '__main__':
    pika.log.setup(level=pika.log.INFO)

    # Connect to RabbitMQ
    host = (len(sys.argv) > 1) and sys.argv[1] or '127.0.0.1'
    connection = SelectConnection(ConnectionParameters(host),
                                  on_connected)
    # Loop until CTRL-C
    try:
        # Start our blocking loop
        connection.ioloop.start()

    except KeyboardInterrupt:

        # Close the connection
        connection.close()

        # Loop until the connection is closed
        connection.ioloop.stop()
开发者ID:shuaizhang15,项目名称:krproject,代码行数:32,代码来源:amqpclient.py

示例6: RouterServer

# 需要导入模块: from pika.adapters import SelectConnection [as 别名]
# 或者: from pika.adapters.SelectConnection import close [as 别名]
class RouterServer(object):
    """Validates and routes notifications.
    
    This acts as a separate server used only for production systems.
    In a development environment, the post office performs the validation
    itself, however this does not allow it to scale in situations where
    a large spike of requests come in (as the requests take longer to
    complete when validation is performed). Thus in production the post
    office simply dumps messages into queue which are then consumed, 
    validated, and then routed to their destination.

    """

    def __init__(self, config):
        self.delivery_conn = None
        self.delivery_channel = None 
        self.notifs_conn = None
        self.notifs_channel = None
        
        # Extract configuration
        self.broker_username = config['broker.username']
        self.broker_password = config['broker.password']
        self.broker_host = config['broker.host']
        self.broker_amqp_port = config['broker.amqp_port']
        self.broker_http_port = config['broker.http_port']
        self.broker_vhost = config['broker.vhost']
        self.incoming_exchange_name = config['broker.incoming_exchange_name']
        self.notifs_queue_name = config['broker.notifications_queue_name']

        # Create connection parameters object for easy reuse
        self.conn_params = pika.ConnectionParameters(
            credentials=pika.PlainCredentials(
                self.broker_username,
                self.broker_password,
            ),
            host=self.broker_host,
            port=self.broker_amqp_port,
            virtual_host=self.broker_vhost,
        )
        
        self.notifs_conn = SelectConnection(
            self.conn_params,
            self.on_notifs_connected,
        )

    @wsgify
    def __call__(self, request):
        """Allows router to be called directly by POST Office to perform
        validation. Intended to simplify development -- SHOULD NOT be 
        used in a production system.

        """
        try:
            self.process_notification(request.body)
        except KeyError as kerr:
            raise HTTPBadRequest()
        except Exception as ex:
            raise HTTPInternalServerError()

        return HTTPAccepted()
        
    # XXX: Ugh...why must Pika be so difficult with multiple connections?
    def start(self, blocking=True):
        #Thread(target=self.delivery_conn.ioloop.start).start()

        if blocking:
            self.notifs_conn.ioloop.start()
        else:
            Thread(target=self.notifs_conn.ioloop.start).start()

    def shutdown(self):
        self.delivery_channel.close()
        self.notifs_channel.close()
        self.delivery_conn.close()
        self.notifs_conn.close()

        # Loop until everything shuts down
        self.notifs_conn.ioloop.start()

    def on_delivery_connected(self, connection):
        connection.channel(self.on_delivery_channel_open)

    def on_delivery_channel_open(self, channel):
        self.delivery_channel = channel

    def on_notifs_connected(self, connection):
        connection.channel(self.on_notifications_channel_open)

        # TODO: Figure out how to get 2 connections working in Pika.
        # This is a hack for now, since we know we only have one broker.
        self.on_delivery_connected(connection)

    def on_notifications_channel_open(self, channel):
        self.notifs_channel = channel
        channel.queue_declare(
            queue=self.notifs_queue_name,
            durable=False,
            exclusive=False,
            auto_delete=False,
            callback=self.on_notifications_queue_declared,
#.........这里部分代码省略.........
开发者ID:psawaya,项目名称:notif,代码行数:103,代码来源:router.py

示例7: Amqp

# 需要导入模块: from pika.adapters import SelectConnection [as 别名]
# 或者: from pika.adapters.SelectConnection import close [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.close方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。