python使用rabbitMQ介绍二(工作队列模式)
2018-07-30 21:01
555 查看
一模式介绍
第一章节的生产-消费者模式,是非常简单的模式,一发一收。在实际的应用中,消费者有的时候需要工作较长的时间,则需要增加消费者。
队列模型:
1 #!/usr/bin/env python 2 import pika 3 import sys 4 5 parameters = pika.ConnectionParameters(host='localhost') 6 connection = pika.BlockingConnection(parameters) 7 8 channel = connection.channel() 9 channel.queue_declare(queue='task_queue', durable=True) 10 11 for i in range(10): 12 message = 'Hello World: {}'.format(i) 13 channel.basic_publish(exchange='', 14 routing_key='task_queue', 15 body=message, 16 properties=pika.BasicProperties(delivery_mode=2)) 17 print(" [x] Sent %r " % message) 18 19 connection.close()View Code
消费者:
#!/usr/bin/env python import pika import time parameters = pika.ConnectionParameters(host='localhost') connection = pika.BlockingConnection(parameters) channel = connection.channel() channel.queue_declare(queue='task_queue', durable=True) print(' [*] Warting for messages. To exit press CTRL+C') def call_back(ch, method, properties, body): print(" [x] Received %r" % body) time.sleep(body.count(b'.')) print(" [x] Done") ch.basic_ack(delivery_tag=method.delivery_tag) channel.basic_qos(prefetch_count=1) channel.basic_consume(call_back, queue='task_queue') channel.start_consuming()View Code
生产者打印输出:
1 Task: 2 [x] Sent 'Hello World: 0' 3 [x] Sent 'Hello World: 1' 4 [x] Sent 'Hello World: 2' 5 [x] Sent 'Hello World: 3' 6 [x] Sent 'Hello World: 4' 7 [x] Sent 'Hello World: 5' 8 [x] Sent 'Hello World: 6' 9 [x] Sent 'Hello World: 7' 10 [x] Sent 'Hello World: 8' 11 [x] Sent 'Hello World: 9'
woerk1输出:
[*] Warting for messages. To exit press CTRL+C [x] Received b'Hello World: 0' [x] Done [x] Received b'Hello World: 2' [x] Done [x] Received b'Hello World: 4' [x] Done [x] Received b'Hello World: 6' [x] Done [x] Received b'Hello World: 7' [x] Done [x] Received b'Hello World: 9' [x] Done
worker二输出:
[*] Warting for messages. To exit press CTRL+C [x] Received b'Hello World: 1' [x] Done [x] Received b'Hello World: 3' [x] Done [x] Received b'Hello World: 5' [x] Done [x] Received b'Hello World: 8' [x] Done
三 队列信息
在web管理页面,可以看到channel情况
在queue页面,可以看到
相关文章推荐
- python使用rabbitmq实例二,工作队列
- python使用rabbitmq实例二,工作队列
- python使用rabbitmq实例二,工作队列 (2)
- python使用rabbitMQ介绍三(发布订阅模式)
- RabbitMQ工作队列之Python实现
- python(十一)上:RabbitMQ 使用详细介绍
- python RabbitMQ队列使用(入门篇)
- RabbitMQ知识盘点【壹】_消息队列介绍及三种消息路由模式
- 基于Python语言使用RabbitMQ消息队列(三)
- python RabbitMQ 使用详细介绍(小结)
- rabbitmq工作队列实现高性能任务的负载分发[python实例] 推荐
- RabbitMQ-三、Java使用--1.工作队列
- rabbitmq工作队列实现高性能任务的负载分发[python实例]
- 基于Python语言使用RabbitMQ消息队列(四)
- 基于Python语言使用RabbitMQ消息队列(二)
- SHT30使用的学习过程1SHT30工作模式介绍
- 基于Python语言使用RabbitMQ消息队列(一)
- RabbitMq六种使用模式(1)_直接指定消息接收队列
- rabbitmq 消息队列工作模式
- RabbitMQ 一二事(2) - 工作队列使用