23213ew 发表于 2016-3-8 10:02:32

python实现rabbitmq客户端消费程序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#coding=utf-8
__author__ = 'chenhuachao'
# --------------------------------
# Created by chenhuachaoon 2015/7/7.
# ---------------------------------
#rabbitmq队列的消费者模式

from pika importconnection,BlockingConnection,ConnectionParameters,credentials,BasicProperties
importtime
def get_rmq_channel(host,port,user,passwd):
    "获取连接到rabbitmq的通道对象"
    conn=BlockingConnection(ConnectionParameters(host=host,port=port,credentials=credentials.PlainCredentials(username=user,password=passwd)))
    returnconn.channel()

channel.queue_declare(queue='statis_cateid_redis_test')#进入需要取数据的队列
print "waiting for n"

def request(ch,method,properties,body): #定义一个回调函数,用来取数据
      print "increase(%s)"%(body,)
conn=get_rmq_channel(host="192.168.3.156",port=5672,user="xxx",passwd="xxx")
conn.basic_consume(request,queue="statis_cateid_redis_test")
conn.start_consuming()


页: [1]
查看完整版本: python实现rabbitmq客户端消费程序