发布者在消耗了第一个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。