RabbitMQ环境搭建好了,接下来就是学习编程的入门级hello world.
在运行程序前,要先确保开启RabbitMQ服务
然后安装pika,命令:pip install pika
1.创建一个python工程,我创建的名为RabitMQ_Demo
2,创建一个send.py
#!/usr/bin/env python3.5.2
# -*- coding: utf-8 -*-import pikaconnection = pika.BlockingConnection(pika.ConnectionParameters(
host='localhost'))
channel = connection.channel()channel.queue_declare(queue='hello')channel.basic_publish(exchange='',
routing_key='hello',
body='Hello World!')
print(" [x] Sent 'Hello World!'")
connection.close()
3,创建一个receive.py
#!/usr/bin/env python3.5.2
# -*- coding: utf-8 -*-import pikaconnection = pika.BlockingConnection(pika.ConnectionParameters(
host='localhost'))
channel = connection.channel()channel.queue_declare(queue='hello')def callback(ch, method, properties, body):
print(" [x] Received %r" % body)channel.basic_consume(callback,
queue='hello',
no_ack=True)print(' [*] Waiting for messages. To exit press CTRL+C')
channel.start_consuming()
4.运行。
(1),先运行receive.py
(2),然后运行send.py
就此,helloworld就完了,接下来,就去网站上去找教程,加深学习。
参考:http://blog.csdn.net/zhangfh1990/article/details/72676411