发布者等待消费消息,同时它应该做两件事(发布和订阅来自订阅者的回复)。

Publisher waits for consuming messages while it should do both things(publishing and subscribing replies from subscriber).)

发布者在使用第一个 message_reply 后被阻止。它没有向订阅者发送任何消息。

我已经尝试过,如果我不使用 'start_consuming()' 方法,发布者会连续发送数据,但不会从订阅者打印 reply_message。 如果我使用 'start_consuming()' 方法,它只会阻止发布者并等待。

#publisher.py
#!/usr/bin/env python
import pika
import sys
import uuid

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

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

message = "Hello World!"
corr_id = str(uuid.uuid4())

def on_response(ch, method, properties, body):
    print("----- On_response -----")
    print("Received CORR_ID : ", properties.correlation_id)
    if(corr_id == properties.correlation_id):
        resp = body.decode('utf-8')
        print("RESPONSE : ", resp)
    ch.basic_ack(delivery_tag=method.delivery_tag)

def consume_response(channel):
    channel.basic_consume(queue='reply_queue', on_message_callback=on_response)
    channel.start_consuming()

while True:
    channel.basic_publish(
        exchange='',
        routing_key='task_queue',
        body=message,
        properties=pika.BasicProperties(
            reply_to='reply_queue',
            correlation_id=corr_id
        ))
    print(" [x] Sent %r" % message)
    consume_response(channel)
#subscriber.py
#!/usr/bin/env python
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.decode('utf-8'))
    #time.sleep(body.count(b'.'))
    print(" [x] Done")
    print("CORR_ID : ",str(properties.correlation_id))
    print("Reply : ", str(properties.reply_to))
    ch.basic_ack(delivery_tag=method.delivery_tag)
    res = "Received "+str(body.decode('utf-8'))
    ch.basic_publish(
        exchange='',
        routing_key='reply_queue',
        properties = pika.BasicProperties(
            correlation_id=\
                properties.correlation_id
        ),
        body=res
    )


channel.basic_qos(prefetch_count=1)
channel.basic_consume(queue='task_queue', on_message_callback=callback)

channel.start_consuming()
Actual result is-
publisher                                           subscriber
   (1)----------publish message 'Hello'-------------->  |
    | <---------reply_to_publisher_queue---------------(2)
   (3)prints_the message
   (4)publisher waits for consuming messages

我要设计这个-

(expected)
publisher                                           subscriber
   (1)----------publish message 'Hello'-------------->  |
    | <---------reply_to_publisher_queue---------------(2)
   (3)prints_the message
   (4)----------publish message 'Hello'-------------->  |
    | <---------reply_to_publisher_queue---------------(5)
    .
    .
    .
   continues....

请帮忙。

我在 publisher.py 中用 channel.connection.process_data_events(time_limit=1) 替换了 channel.start_consuming() 行,它按我预期的那样工作。

我也关注了https://github.com/pika/pika/issues/770