如果不与block一起使用,则使用Rabbitpy发布到不存在的交换不会引发异常:
>>> conn = rabbitpy.Connection('amqp://LenelRabbit:Secur1ty%23@ms7:5672/LenelHost')
>>> ch = conn.channel()
>>> msg = rabbitpy.Message(ch, body_value='hello')
>>> msg.publish(exchange='invalid')
>>>
使用Channel作为上下文管理器确实会引发错误:
>>> conn = rabbitpy.Connection('amqp://myuser:mypass@ms7:5672/myhost')
>>> with conn.channel() as ch:
... msg = rabbitpy.Message(ch, body_value='hello')
... msg.publish(exchange='invalid')
...
Traceback (most recent call last):
File "<stdin>", line 3, in <module>
File "D:\Programming\mqclient\env36x64\lib\site-packages\rabbitpy\channel.py", line 101, in __exit__
self.close()
File "D:\Programming\mqclient\env36x64\lib\site-packages\rabbitpy\channel.py", line 147, in close
super(Channel, self).close()
File "D:\Programming\mqclient\env36x64\lib\site-packages\rabbitpy\base.py", line 204, in close
self._check_for_pending_frames()
File "D:\Programming\mqclient\env36x64\lib\site-packages\rabbitpy\base.py", line 318, in _check_for_pending_
frames
self._check_for_rpc_request(value)
File "D:\Programming\mqclient\env36x64\lib\site-packages\rabbitpy\channel.py", line 271, in _check_for_rpc_r
equest
super(Channel, self)._check_for_rpc_request(value)
File "D:\Programming\mqclient\env36x64\lib\site-packages\rabbitpy\base.py", line 328, in _check_for_rpc_requ
est
self._on_remote_close(value)
File "D:\Programming\mqclient\env36x64\lib\site-packages\rabbitpy\base.py", line 380, in _on_remote_close
raise exceptions.AMQP[value.reply_code](value)
rabbitpy.exceptions.AMQPNotFound: <pamqp.specification.Channel.Close object at 0x333e438>