RabbitMQ: How to send Python dictionary between Python producer and consumer?

You can’t send native Python types as your payload, you have to serialize them first. I recommend using JSON:

import json
channel.basic_publish(exchange="",
                      routing_key='task_queue',
                      body=json.dumps(message),
                      properties=pika.BasicProperties(
                          delivery_mode = 2, # make message persistent
                      ))

and

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

Leave a Comment