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


Python Connection.open方法代码示例

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


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

示例1: test_connection_open

# 需要导入模块: from amqpstorm import Connection [as 别名]
# 或者: from amqpstorm.Connection import open [as 别名]
    def test_connection_open(self):
        connection = Connection('127.0.0.1', 'guest', 'guest', lazy=True)
        io = IO(connection.parameters, [])
        io.socket = Mock(name='socket', spec=socket.socket)
        connection._io = io

        def open():
            pass

        def on_write_to_socket(_):
            connection.set_state(connection.OPEN)

        connection._io.open = open
        connection._io.write_to_socket = on_write_to_socket

        self.assertTrue(connection.is_closed)

        connection.open()

        self.assertTrue(connection.is_open)
开发者ID:eandersson,项目名称:amqpstorm,代码行数:22,代码来源:connection_tests.py

示例2: Publisher

# 需要导入模块: from amqpstorm import Connection [as 别名]
# 或者: from amqpstorm.Connection import open [as 别名]
class Publisher(object):
    def __init__(self, host, username, password):
        self.channel = None
        self.connection = None
        self.host = host
        self.username = username
        self.password = password
        self.connect()

    def connect(self):
        self.connection = Connection(self.host, self.username, self.password)
        self.channel = self.connection.channel()

    def close_connection(self):
        self.channel.close()
        self.connection.close()

    def send_message(self, queue, message):
        if self.connection.is_closed:
            self.reconnect()
        try:
            self.channel.basic.publish(body=message,
                                       routing_key=queue)
        except AMQPError as why:
            # When handling AMQPError's here, be careful as you may
            # need to re-send the payload.
            print(why)
            self.reconnect()

    def reconnect(self):
        """ Re-connect.

        :return:
        """
        try:
            if self.connection.is_closed:
                self.connection.open()
            if self.channel.is_closed:
                self.channel.open()
        except AMQPError:
            raise
开发者ID:timothy-hanson,项目名称:amqp-storm,代码行数:43,代码来源:basic_error_handling.py

示例3: OpenCloseOpenCloseTest

# 需要导入模块: from amqpstorm import Connection [as 别名]
# 或者: from amqpstorm.Connection import open [as 别名]
class OpenCloseOpenCloseTest(unittest.TestCase):
    def setUp(self):
        self.connection = Connection(HOST, USERNAME, PASSWORD, lazy=True)

    def test_open_close_loop(self):
        for _ in range(100):
            self.connection.open()
            self.channel = self.connection.channel()

            # Verify that the Connection/Channel has been opened properly.
            self.assertIsNotNone(self.connection._io.socket)
            self.assertIsNotNone(self.connection._io.poller)
            self.assertTrue(self.channel.is_open)
            self.assertTrue(self.connection.is_open)

            self.channel.queue.declare('test.open.close')
            self.channel.basic.publish(body=str(uuid.uuid4()),
                                       routing_key='test.open.close')
            self.channel.close()
            self.connection.close()

            # Verify that the Connection/Channel has been closed properly.
            self.assertTrue(self.channel.is_closed)
            self.assertTrue(self.connection.is_closed)
            self.assertIsNone(self.connection._io.socket)
            self.assertIsNone(self.connection._io.poller)

        time.sleep(0.1)

        self.assertEqual(threading.activeCount(), 1,
                         msg='Current Active threads: %s'
                             % threading._active)

    def tearDown(self):
        self.connection = Connection(HOST, USERNAME, PASSWORD)
        self.channel = self.connection.channel()
        self.channel.queue.delete('test.open.close')
        self.channel.close()
        self.connection.close()
开发者ID:exg77,项目名称:amqpstorm,代码行数:41,代码来源:generic_tests.py


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