I am trying to send a python dictionary from a python producer to a python consumer using RabbitMQ. First, the manufacturer establishes a connection with the local RabbitMQ server. Then it creates a queue to which the message will be delivered, and finally sends the message. First, the consumer connects to the RabbitMQ server, and then is sure that the queue exists, creating the same queue. Then it receives a message from the manufacturer in the callback function and prints the value "id" (1). Here are the scenarios for producer and consumer:
producer .py script:
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,
consumer.py script:
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()
But when I run the manufacturer.py file, I get this error:
line 18, in <module> 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
Can someone help me? Thanks!