本文地址:http://blog.csdn.net/spch2008/article/details/11569127
from kombu.entity import Exchange, Queue from kombu.messaging import Consumer from kombu.connection import Connection connection = Connection('amqp://guest:[email protected]:5672//') channel = connection.channel() news_exchange = Exchange('news', type='topic') bound_exchange = news_exchange(channel) bound_exchange.declare()第9行声明一个Exchange,并不能再上面执行任何操作,只是描述了一下这个Exchange的特性。
第10行将exchange与特定的channel进行绑定。内部其实只是复制了自身,然后将_channel赋值而已。(Exchange内部记住了channel)
def __call__(self, channel): return self.bind(channel) def bind(self, channel): return copy(self).maybe_bind(channel) def maybe_bind(self, channel): if not self.is_bound and channel: self._channel = channel self.when_bound() self._is_bound = True return self def when_bound(self): pass第11行,在服务器上创建Exchange。这样,通过命令rabbitmqctl list_exchanges就可以看到创建的exchange。
class kombu.Exchange(name='', type='', channel=None, **kwargs) Parameters: name - Exchange名字 type – direct, fanout,topic,header几种类型 channel – Exchange需要绑定的channel durable – server重启后是否恢复Exchange,默认True,即重启后Exchange仍然存在 delivery_mode – transient模式,消息只存储于内存server重启或死机,消息丢失。 persistent模式,消息存在内存与硬盘,信息不丢失,默认persistent。
declare(self, nowait=False) server上创建Exchange nowait True 不等待Server的响应信息。 delete(self, if_unused=False, nowait=False) server上删除Exchange if_unused True只在Exchange没有使用的时候删除