python rabbitmq消费者返回数据_RabbitMQ:如何在Python生产者和消费者之间发送Python字典?...

我试图使用RabbitMQ将python字典从python生产者发送给python消费者。生产者首先建立到本地RabbitMQ服务器的连接。然后它创建一个队列,将消息传递到该队列,最后发送消息。使用者首先连接到RabbitMQ服务器,然后通过创建相同的队列来确保队列存在。然后,它从回调函数中的producer接收消息,并打印“id”值(1)。以下是制作人和消费者的脚本:

producer.py脚本:import pika

import sys

connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))

channel = connection.channel()

channel.queue_declare(queue='task_queue', durable=True)

message = {'id': 1, 'name': 'name1'}

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()

consumer.py脚本: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)

print(body['id'])

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()

但是,当我运行producer.py时,会出现以下错误:line 18, in

delivery_mode = 2, # make message persistent

File "/Library/Python/2.7/site-packages/pika/adapters/blocking_connection.py", line 1978, in basic_publish

mandatory, immediate)

File "/Library/Python/2.7/site-packages/pika/adapters/blocking_connection.py", line 2064, in publish

immediate=immediate)

File "/Library/Python/2.7/site-packages/pika/channel.py", line 338, in basic_publish

(properties, body))

File "/Library/Python/2.7/site-packages/pika/channel.py", line 1150, in _send_method

self.connection._send_method(self.channel_number, method_frame, content)

File "/Library/Python/2.7/site-packages/pika/connection.py", line 1571, in _send_method

self._send_message(channel_number, method_frame, content)

File "/Library/Python/2.7/site-packages/pika/connection.py", line 1596, in _send_message

content[1][s:e]).marshal())

TypeError: unhashable type

有人能帮我吗?谢谢!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值