码迷,mamicode.com
首页 > 其他好文 > 详细

RabbitMq-初学

时间:2021-01-13 10:59:28      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:oid   cas   routing   def   dir   world   size   font   lazy   

在操作pika时请记得安装RabbitMQ 如果没有安装会有

技术图片

 

 

send端

import pika

connection = pika.BlockingConnection(pika.ConnectionParameters(‘localhost‘))
channel = connection.channel()

# 声明queue
channel.queue_declare(queue=‘hello‘)

# n RabbitMQ a message can never be sent directly to the queue, it always needs to go through an exchange.
channel.basic_publish(exchange=‘‘,
routing_key=‘hello‘,
body=‘Hello World!‘)
print(" [x] Sent ‘Hello World!‘")
connection.close()

receive端
import pika
import queue
connection = pika.BlockingConnection(pika.ConnectionParameters(
‘localhost‘))
channel = connection.channel()

# You may ask why we declare the queue again ? we have already declared it in our previous code.
# We could avoid that if we were sure that the queue already exists. For example if send.py program
# was run before. But we‘re not yet sure which program to run first. In such cases it‘s a good
# practice to repeat declaring the queue in both programs.
channel.queue_declare(queue=‘hello‘)


def callback(ch, method, properties, body):
print(" [x] Received %r" % body)


channel.basic_consume(queue="hello", on_message_callback=callback, auto_ack=True)

print(‘ [*] Waiting for messages. To exit press CTRL+C‘)
channel.start_consuming()
############在执行job时抄别人得 channel,basic_cosum
(queue="hello",callback, ack=True)####################
报错说我参数有问题类型报错

RabbitMq-初学

标签:oid   cas   routing   def   dir   world   size   font   lazy   

原文地址:https://www.cnblogs.com/quemengqio/p/14262644.html

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