python rabbitmq
#!/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的更多相关文章
- Python之路-python(rabbitmq、redis)
一.RabbitMQ队列 安装python rabbitMQ module pip install pika or easy_install pika or 源码 https://pypi.pytho ...
- python RabbitMQ队列/redis
RabbitMQ队列 rabbitMQ是消息队列:想想之前的我们学过队列queue:threading queue(线程queue,多个线程之间进行数据交互).进程queue(父进程与子进程进行交互或 ...
- python RabbitMQ队列使用(入门篇)
---恢复内容开始--- python RabbitMQ队列使用 关于python的queue介绍 关于python的队列,内置的有两种,一种是线程queue,另一种是进程queue,但是这两种que ...
- Python RabbitMQ消息队列
python内的队列queue 线程 queue:不同线程交互,不能夸进程 进程 queue:只能用于父进程与子进程,或者同一父进程下的多个子进程,进行交互 注:不同的两个独立进程是不能交互的. ...
- python RabbitMQ队列使用
python RabbitMQ队列使用 关于python的queue介绍 关于python的队列,内置的有两种,一种是线程queue,另一种是进程queue,但是这两种queue都是只能在同一个进程下 ...
- python Rabbitmq编程(一)
python Rabbitmq编程(一) 实现最简单的队列通信 send端 #!/usr/bin/env python import pika credentials = pika.PlainCred ...
- Python—RabbitMQ
RabbitMQ RabbitMQ是一个在AMQP基础上完整的,可复用的企业消息系统 安装 因为RabbitMQ由erlang实现,先安装erlang #安装配置epel源 rpm -ivh http ...
- Python RabbitMQ消息持久化
RabbitMQ消息持久化:就是将队列中的消息永久的存放在队列中. 处理方案: # 在实例化时加入durable=True来确认消息的实例化,客户端服务端都要写 channel.queue_dec ...
- 初学Python——RabbitMQ的安装
记录踩坑之路,本篇文章主要摘抄自CSDN博客https://blog.csdn.net/weixin_39735923/article/details/79288578 Windows10环境下安装R ...
随机推荐
- PHP计算程序运行时间的类
<?php class runTime{ private $starTime; private $stopTime; private function getMicTime(){ $mictim ...
- [Surface] 在win8.1上使用QQ截图放大问题(解决办法)
在使用每次截图的时候整个都被放大了,很让人郁闷,截不到完整的图,本着遇到问题解决问题的想法,这事早解决早好. 开工: 1. 度娘上搜索"win8 qq截图 放大",找到很多资料 ...
- POJ 1947 Rebuilding Roads 树形DP
Rebuilding Roads Description The cows have reconstructed Farmer John's farm, with its N barns (1 & ...
- 如何在Ubuntu中让mongo远程可连接
最近团队的一个成员由于项目原因需要在vps上建立mongo数据库服务器并允许远端访问,这里整理下设置的思路 首先需要安装mongo apt-get updateapt-get install mong ...
- pythonchallenge之C++学习篇-03
提示说一个小写字母两面精确地被大写字母包围,应该指的是周围没有四个而仅仅这两个像这样的:xXXXxXXXx的中间的那个应该是符合条件的 好了标题是re,提示该是使用正则表达式,网页源码里有待处理的字符 ...
- 软引用SoftReference异步加载图片
HashMap<String, SoftReference<Drawable>> imageCache 关于SoftReference这个类多少知道些机制,会用就ok了. 机制 ...
- css3易混淆属性详解
1.background, background-color, color (1)background:在一个声明中设置所有属性: 如:background: #00FF00 url(bgima ...
- shell-bash学习01基础、打印、环境变量
基础 终端提示符: username@hostname$; $: 一般用户 #:root用户 运行脚本 Bash执行: * bash script.sh; 直接运行: 脚本开头添加shebang起始: ...
- 07 JavaWeb
软件开发的两种架构:c/s和b/s * C/S client/server 客户端/服务器 例子:QQ 快播 暴风影音... ...
- 阻止Ajax多次提交
1.Ajax的abort() xhr = $.ajax({}) if (xhr){ xhr.abort(); } 2.通过在Ajax的beforeSend()方法以及complete()方法添加删除类 ...