RabbitMQ安装见另外一篇文章python rpc framework ---- callme/multiprocessing.managers
分享 http://download.csdn.net/detail/xiarendeniao/5781457
1.概念:
channel 我理解就是每个连接对应的一个操作标识(A Channel is the primary communication method for interacting with RabbitMQ. It is recommended that you do not directly invokethe creation of a channel object in your application code but ratherconstruct the a channel by calling the active connection’s channel()method.)2.六种使用模式 见图“rabbitmq-1.jpg”
当多个消费者连接到同一个queue时,queue里面同一个消息只会被一个消费者拿到擦,去年在互动百科做那个相关性分析项目时就有这个问题,一直以为无解呢!当时用的是activemq(见另一篇文章python--memcached---activemq),当一个消费者程序启动比较快,另几个启动晚一些的时候,queue里面的几个任务就都被先报到的消费者预订了。结果是先到的消费者累死累活,后到的消费者闲着没事干。
对于模式6,不明白correlation_id和reply_to有什么必要性,返回结果的queue和message的标识由业务逻辑控制打入message内部完全没问题嘛...这两个参数反正是由服务端(姑且这样称呼吧,其实也是一个从queue中取message做计算并往另一个queue塞结果message的程序)的业务逻辑解析的,又不是RabbitMQ内部针对这两个属性会做什么处理!相比而言,我觉得delivery_mode才有意义,RabbitMQ会把指定该属性为2的message持久化到硬盘上。
官网说的解决办法是对于发数据的程序(生产者)需要做事务处理(channel使用transaction模式),或者使用确认机制(confirm mode)。且,两者不可同时使用(http://www.rabbitmq.com/confirms.html)
客户端
1.AMQP客户端如何获取Queue中消息的数量?
import pika def on_callback(msg): print msg params = pika.ConnectionParameters( host='localhost', port=5672, credentials=pika.credentials.PlainCredentials('guest', 'guest'), ) # Open a connection to RabbitMQ on localhost using all default parameters connection = pika.BlockingConnection(parameters=params) # Open the channel channel = connection.channel() # Declare the queue channel.queue_declare( callback=on_callback, queue="test", durable=True, exclusive=False, auto_delete=False ) # ... # Re-declare the queue with passive flag channel.queue_declare( callback=on_callback, queue="test", durable=True, exclusive=False, auto_delete=False, passive=True )
<Method(['frame_type=1', 'channel_number=1', "method=<Queue.DeclareOk(['queue=test', 'message_count=0', 'consumer_count=0'])>"])> <Method(['frame_type=1', 'channel_number=1', "method=<Queue.DeclareOk(['queue=test', 'message_count=0', 'consumer_count=0'])>"])>http://stackoverflow.com/questions/8192584/get-queue-size-in-pika-amqp-python