[英]Rabbit MQ StreamLostError with python pika library
When I listen my queue with python pika library, I always get StreamLostError and my code crushes.当我使用 python 鼠兔库收听我的队列时,我总是得到 StreamLostError 并且我的代码崩溃了。
In my code, I must listen the queue forever without exception and I must get messages 1 by 1.在我的代码中,我必须无一例外地永远监听队列,并且我必须一条一条地获取消息。
Here is my code(I simplified it).这是我的代码(我简化了它)。
def callback(ch, method, properties, body):
ch.basic_ack(delivery_tag = method.delivery_tag)
#doing work here, it gets minimum 5 minutes, sometimes maximum 1 hour
credentials = pika.PlainCredentials(username, password)
parameters = pika.ConnectionParameters(ip, port, '/', credentials)
connection = pika.BlockingConnection(parameters)
channel = connection.channel()
channel.basic_qos(prefetch_count=1)
channel.queue_declare(queue=queuename, durable=True)
channel.basic_consume(queue=queuename, on_message_callback=callback, auto_ack=False)
channel.start_consuming()
try to set connection_attempts and retry_delay parameters in the request if you are using pika URLParameters.如果您使用 pika URLParameters,请尝试在请求中设置 connection_attempts 和 retry_delay 参数。 Look down the below link for more information.In my case I added ?connection_attempts=20&retry_delay=1
after the AMPQ https://pika.readthedocs.io/en/stable/modules/parameters.html#urlparameters查看以下链接以获取更多信息。在我的例子中,我在 AMPQ https 之后添加了?connection_attempts=20&retry_delay=1
://pika.readthedocs.io/en/stable/modules/parameters.html#urlparameters
The problem is that your work takes too long and blocks Pika's I/O loop.问题是你的工作时间太长并且阻塞了 Pika 的 I/O 循环。 This causes heartbeats to be missed, and RabbitMQ thinks your connection is dead.这会导致心跳丢失,RabbitMQ 认为您的连接已死。
Please see this code for one correct way to do long-running work:请参阅此代码以了解进行长时间运行的一种正确方法:
https://github.com/pika/pika/blob/master/examples/basic_consumer_threaded.py https://github.com/pika/pika/blob/master/examples/basic_consumer_threaded.py
NOTE: the RabbitMQ team monitors the rabbitmq-users
mailing list and only sometimes answers questions on StackOverflow.注意: RabbitMQ 团队会监控rabbitmq-users
邮件列表,并且只是偶尔在 StackOverflow 上回答问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.