如果连接空闲/长时间不使用,则停止channel.basic_consume [英] Stop channel.basic_consume if the connection is idle/Not consuming from long time

查看:219
本文介绍了如果连接空闲/长时间不使用,则停止channel.basic_consume的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用例,我想得到一个pika使用者(pika.BlockingConnection)的最后一个空闲时间(最后一个消息处理时间).
用例:
如果最后处理时间大于阈值时间(例如:1小时).我想让消费者退出还是可以使用回调方法来决定我需要做什么?就像向用户发送通知一样.

I am having a use case in which i want get the last idle time(last message processed time) of a pika consumer(pika.BlockingConnection).
Usecase:
If the last processed time is greater than Threshold time(ex: 1 hr). I want the consumer to get exited or have a callback method to decide on what i need to do? Like sending a notification to a user.

有没有办法做到这一点?

Is there any way to do this?

推荐答案

pika支持您可以在每个消息接收的末尾添加此回调,保留对其的引用,然后在每个消息接收的开始处将其删除.

You could add this callback at the end of each message receipt, keeping the reference to it, and removing it at the start of each message receipt.

def close_connec():
    # close here

timer_id = None

def on_message(chan, method, props, body):
    if timer_id is not None:
        chan.connection.remove_timeout(timer_id)
    # process message
    timer_id = chan.connection.add_timeout(3600, close_connec)

这篇关于如果连接空闲/长时间不使用,则停止channel.basic_consume的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆