Stop channel.basic_consume if the connection is idle/Not consuming from long time

与世无争的帅哥 提交于 2019-12-11 12:54:18

问题


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?


回答1:


pika supports a timeout callback.

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)


来源:https://stackoverflow.com/questions/37884655/stop-channel-basic-consume-if-the-connection-is-idle-not-consuming-from-long-tim

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!