码迷,mamicode.com
首页 > 编程语言 > 详细

python 学习总结3 消息队列RabbitMQ

时间:2017-09-07 19:24:33      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:一个队列   链接   call   消息队列   routing   print   block   nec   一个   

我们以前学过的队列,在线程中针对同一程序下的多个线程直接 可以实现消息的发送接收,对于进程来说只能在父进程与子进程中或者 父进程 下的子进程之间 进行,都无法实现连个进程的交互

RabbitMQ 实现了这一功能

需要先下载RabbitMQ 之后还需要下载erlang语言,因为RabbitMQ就是由erlang编辑而成的!

生产者:

import pika
#我们先生成一个链接的实例
connection=pika.BlockingConnection(pika.ConnectionParameters("localhost"))
#开通一个管道
channer=connection.channel()
#给管道一个队列,队列的名字叫Hello
channer.queue_declare(queue="hello")
#在管道中需要传输的数据格式
channer.basic_publish(exchange="",routing_key="hello",body="hello world!")
print(" sent the word!")
connection.close()

消费者:

import pika
#这三步骤 其实 两个里面都是一样的
connection=pika.BlockingConnection(pika.ConnectionParameters("localhost"))

channer=connection.channel()

channer.queue_declare("hello")


def callback(ch,menthod,properties,body):
    print("recevied %s"%body)

channer.basic_consume(callback,queue="hello",no_ack=True)

print("waiting for message ")
channer.start_consuming()

 

python 学习总结3 消息队列RabbitMQ

标签:一个队列   链接   call   消息队列   routing   print   block   nec   一个   

原文地址:http://www.cnblogs.com/shidi/p/7491020.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!