#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
@author: zengchunyun
"""
import pika class MQServer(object):
def __init__(self, host, port=5672, exchange=None, exchange_type="topic"):
"""
初始化MQ设置
:param host: MQ服务器地址
:param port: MQ端口
:param exchange: 交换器名称
:param exchange_type: 交换器类型,默认关键字类型
:return:
"""
self.host = host
self.port = port
self.exchange = exchange
self.exchange_type = exchange_type
self.queue = None
self.connection = self.connect()
self.channel = self.connect_channel()
self.create_exchange() def connect(self):
"""
连接MQ服务器
:return:
"""
return pika.BlockingConnection(pika.ConnectionParameters(host=self.host, port=self.port)) def connect_channel(self):
"""
创建频道
:return:
"""
return self.connection.channel() def create_exchange(self):
"""
定义交换器名称,防止发布时,如果交换器不存在,异常
:return:
"""
self.channel.exchange_declare(exchange=self.exchange, type=self.exchange_type) def publish(self, exchange=None, routing_key=None, body=None):
"""
创建发布者
:param exchange: 交换器名称
:param routing_key: 路由KEY
:param body:消息主体
:return:
"""
if exchange:
self.exchange = exchange
self.channel.basic_publish(exchange=self.exchange, routing_key=routing_key, body=body)
self.close() def consumer(self, exchange=None, routing_key=None, callback=None):
"""
创建消费者
:param exchange:
:param routing_key:
:param callback:
:return:
"""
if exchange:
self.exchange = exchange
self.create_queue()
self.channel.queue_bind(queue=self.queue, exchange=self.exchange, routing_key=routing_key)
self.channel.basic_consume(consumer_callback=callback, queue=self.queue, no_ack=True)
self.start() def create_queue(self):
"""
生成队列,当关闭consumer时,加上exclusive=True,queue也会被删除
:return:
"""
self.queue = self.channel.queue_declare(exclusive=True).method.queue # 为每个消费者生成不同的队列 def close(self):
"""
关闭消息连接
:return:
"""
self.connection.close() def start(self):
self.channel.start_consuming()

1.消息持久化存储

  虽然有了消息反馈机制,但如果rabbitmq自身挂掉的话,那么任务还是会丢失,所以需要将任务持久化存储起来,

durable=True  # 开启持久化设置,rabbitmq不允许使用不同的参数来重新定义存在的队列

self.queue = self.channel.queue_declare(exclusive=True,durable=True)  
self.channel.exchange_declare(exchange=self.exchange, type=self.exchange_type, durable=True)
在发送任务的时候,用delivery_mode=2来标记任务为持久化存储
 self.channel.basic_publish(exchange='',
routing_key=routing_key,
body=message,
properties=pika.BasicProperties(
delivery_mode = 2, # make message persistent
))

2.公平调度(fair dispatch)

虽然每个工作者是依次分配到任务,但是每个任务不一定一样,可能有到任务比较重,执行时间长,有的任务比较轻,执行时间短,如果能公平调度最好了,使用basic_qos设置prefetch_count=1,使得rabbitmq不会在同一时间给工作者分配多个任务,即只有工作者完成任务之后,才会再次接收到任务

 channel.basic_qos(prefetch_count=1)

完整示例代码

 #!/usr/bin/env python
import pika
import sys connection = pika.BlockingConnection(pika.ConnectionParameters(
host='localhost'))
channel = connection.channel() channel.queue_declare(queue='task_queue', durable=True) message = ' '.join(sys.argv[1:]) or "Hello World!"
channel.basic_publish(exchange='',
routing_key='task_queue',
body=message,
properties=pika.BasicProperties(
delivery_mode = 2, # make message persistent
))
print (" [x] Sent %r" % (message,))
connection.close()

消费者代码

 #!/usr/bin/env python
import pika
import time connection = pika.BlockingConnection(pika.ConnectionParameters(
host='localhost'))
channel = connection.channel() channel.queue_declare(queue='task_queue', durable=True)
print( ' [*] Waiting for messages. To exit press CTRL+C') def callback(ch, method, properties, body):
print (" [x] Received %r" % (body,))
time.sleep( body.count('.') )
print (" [x] Done")
ch.basic_ack(delivery_tag = method.delivery_tag) channel.basic_qos(prefetch_count=1)
channel.basic_consume(callback,
queue='task_queue') channel.start_consuming()
 

python rabbitmq的更多相关文章

  1. Python之路-python(rabbitmq、redis)

    一.RabbitMQ队列 安装python rabbitMQ module pip install pika or easy_install pika or 源码 https://pypi.pytho ...

  2. python RabbitMQ队列/redis

    RabbitMQ队列 rabbitMQ是消息队列:想想之前的我们学过队列queue:threading queue(线程queue,多个线程之间进行数据交互).进程queue(父进程与子进程进行交互或 ...

  3. python RabbitMQ队列使用(入门篇)

    ---恢复内容开始--- python RabbitMQ队列使用 关于python的queue介绍 关于python的队列,内置的有两种,一种是线程queue,另一种是进程queue,但是这两种que ...

  4. Python RabbitMQ消息队列

    python内的队列queue 线程 queue:不同线程交互,不能夸进程 进程 queue:只能用于父进程与子进程,或者同一父进程下的多个子进程,进行交互 注:不同的两个独立进程是不能交互的.   ...

  5. python RabbitMQ队列使用

    python RabbitMQ队列使用 关于python的queue介绍 关于python的队列,内置的有两种,一种是线程queue,另一种是进程queue,但是这两种queue都是只能在同一个进程下 ...

  6. python Rabbitmq编程(一)

    python Rabbitmq编程(一) 实现最简单的队列通信 send端 #!/usr/bin/env python import pika credentials = pika.PlainCred ...

  7. Python—RabbitMQ

    RabbitMQ RabbitMQ是一个在AMQP基础上完整的,可复用的企业消息系统 安装 因为RabbitMQ由erlang实现,先安装erlang #安装配置epel源 rpm -ivh http ...

  8. Python RabbitMQ消息持久化

    RabbitMQ消息持久化:就是将队列中的消息永久的存放在队列中.   处理方案: # 在实例化时加入durable=True来确认消息的实例化,客户端服务端都要写 channel.queue_dec ...

  9. 初学Python——RabbitMQ的安装

    记录踩坑之路,本篇文章主要摘抄自CSDN博客https://blog.csdn.net/weixin_39735923/article/details/79288578 Windows10环境下安装R ...

随机推荐

  1. 跟着鸟哥学Linux系列笔记0-扫盲之概念

    相关缩写全称: POSIX(Portable Operation System Interface):可携式操作系统接口,重点在于规范内核与应用之间的接口,由IEEE定义发布 IEEE: 美国电气与电 ...

  2. Matlab中如何将(自定义)函数作为参数传递给另一个函数

    假如我们编写了一个积分通用程序,想使它更具有通用性,那么可以把被积函数也作为一个参数.在c/c++中,可以使用函数指针来实现上边的功能,在matlab中如何实现呢?使用函数句柄--这时类似于函数指针的 ...

  3. MapKit 添加大头针

    #import "ViewController.h" #import <MapKit/MapKit.h> #import "MYAnnotation.h&qu ...

  4. Windows7系统主题制作全程教程

    jpg 改 rar

  5. python 的特殊方法 __str__和__repr__

    __str__和__repr__ 如果要把一个类的实例变成 str,就需要实现特殊方法__str__(): class Person(object): def __init__(self, name, ...

  6. CSS中margin和padding的区别

    在CSS中margin是指从自身边框到另一个容器边框之间的距离,就是容器外距离.在CSS中padding是指自身边框到自身内部另一个容器边框之间的距离,就是容器内距离. 下面讲解 padding和ma ...

  7. python中的变量和数据类型

    一.变量定义:变量是计算机内存中的一块区域,存储规定范围内的值,值 可以改变,通俗的说变量就是给数据起个名字. 二.变量命名规则: 1. 变量名由字母.数字.下划线组成 2. 数字不能开头 3. 不可 ...

  8. hdu2859 dp

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=2859 题意:输入一个数n,接下来是一个由n*n个字母组成的矩阵,求以左下到右上的线为轴的最 ...

  9. hdu5092 dp(递推)

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5092 题意:给一个m*n的矩阵,找到一个纵向的"线"使得线上的和最小并 ...

  10. Hdu2544 最短路径 四种方法

    Problem Description 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt.但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以现在他们想要 ...