本文整理汇总了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)
示例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
示例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()