RabbitMQ: Python プロデューサーとコンシューマーの間で Python 辞書を送信するにはどうすればいいですか? 質問する

RabbitMQ: Python プロデューサーとコンシューマーの間で Python 辞書を送信するにはどうすればいいですか? 質問する

私は、RabbitMQ を使用して、Python プロデューサーから Python コンシューマーに Python 辞書を送信しようとしています。プロデューサーは最初にローカル RabbitMQ サーバーへの接続を確立します。次に、メッセージが配信されるキューを作成し、最後にメッセージを送信します。コンシューマーは最初に RabbitMQ サーバーに接続し、同じキューを作成してキューが存在することを確認します。次に、コールバック関数内でプロデューサーからメッセージを受信し、「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 <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

誰か助けてくれませんか? ありがとう!

ベストアンサー1

ネイティブ Python 型をペイロードとして送信することはできません。まずシリアル化する必要があります。JSON を使用することをお勧めします。

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

そして

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

おすすめ記事