rabbitmq(pika)在使用RPC时抛出异常

2024-04-26 19:11:32 发布

您现在位置:Python中文网/ 问答频道 /正文

当我构建发布者-消费者模式时,rabbiq-RPC抛出了一个异常。这是我的代码: 发件人.py在

# coding:utf-8
import pika
import uuid


class MessageSender(object):

    def __init__(self):
        self.connention = pika.BlockingConnection(
            pika.ConnectionParameters(host='localhost'))
        self.channel = self.connention.channel()

        result = self.channel.queue_declare(exclusive=True)
        self.callback_queue = result.method.queue
        print 'MessageSender callback queue is ------', self.callback_queue
        self.channel.basic_consume(
            self.on_response, no_ack=True, queue=self.callback_queue)

    def on_response(self, ch, method, props, body):
        if self.corr_id == props.correlation_id:
            self.respose = body

    def send(self):
        self.respose = None
        self.corr_id = str(uuid.uuid4())
        self.channel.basic_publish(exchange='',
                                   routing_key='rpc_queue',
                                   properties=pika.BasicProperties(
                                       reply_to=self.callback_queue,
                                       correlation_id=self.corr_id,),
                                   body='MESSAGE')
        print '[x]Send sucessful'
        while self.respose is None:
            self.connention.process_data_events()
        print 'MessageSender get callback ------', self.respose


if __name__ == '__main__':
    sender = MessageSender()
    sender.send()

在工人.py在

^{pr2}$

发送者工作正常,但是工人.py总是给出错误:

[x]Waiting message...
MessageWorker get message ------ MESSAGE
MessageWorker callback on queue ------ amq.gen-6l5oJapbiKIqG3ZYTRwqpA
MessageWorker send message to MessageSender
Traceback (most recent call last):
  File "python/workers/new_worker.py", line 40, in <module>
    worker.work()
  File "python/workers/new_worker.py", line 30, in work
    self.channel.start_consuming()
  File "/usr/local/lib/python2.7/dist-packages/pika/adapters/blocking_connection.py", line 955, in start_consuming
    self.connection.process_data_events()
  File "/usr/local/lib/python2.7/dist-packages/pika/adapters/blocking_connection.py", line 243, in process_data_events
    raise exceptions.ConnectionClosed()
pika.exceptions.ConnectionClosed

这让我烦恼了将近一个星期!有人能帮忙吗?谢谢:)


Tags: inpyselfidqueuedeflinecallback