终止挂起的redis pubsub.listen()线程 [英] Terminate a hung redis pubsub.listen() thread

查看:358
本文介绍了终止挂起的redis pubsub.listen()线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题有关,我有以下代码订阅redis pubsub队列,并使用__init__中提供的处理程序将消息提供给处理消息的类:

Related to this question I have the following code which subscribes to a redis pubsub queue and uses the handler provided in __init__ to feed the messages to the class that processes them:

from threading import Thread
import msgpack

class Subscriber(Thread):

    def __init__(self, redis_connection, channel_name, handler):
        super(Subscriber, self).__init__(name="Receiver")
        self.connection = redis_connection
        self.pubsub = self.connection.pubsub()
        self.channel_name = channel_name
        self.handler = handler
        self.should_die = False

    def start(self):
        self.pubsub.subscribe(self.channel_name)
        super(Subscriber, self).start()

    def run(self):
        for msg in  self.pubsub.listen():
            if self.should_die:
                return
            try:
                data = msg["data"]
                unpacked = msgpack.unpackb(data)
            except TypeError:
                # stop non-msgpacked, invalid, messages breaking stuff
                # other validation happens in handler
                continue
            self.handler(unpacked)

    def die(self):
        self.should_die = True

在上面的链接问题中,请注意,如果断开连接,则pubsub.listen()从不返回.因此,虽然可以调用我的die()函数,但它实际上不会导致线程终止,因为它挂在线程run()内部对listen()的调用上.

In the linked question above, it is noted that pubsub.listen() never returns if the connection is dropped. Therefore, my die() function, while it can be called, will never actually cause the thread to terminate because it is hanging on the call to listen() inside the thread's run().

有关链接问题的公认答案提到黑客入侵redis-py的连接池.我真的不想这样做,并且有一个分支版本的redis-py(至少在希望该修补程序被母版接受之前),但是无论如何我一直在看一下redis-py代码,并且不要立即看看将在哪里进行更改.

The accepted answer on the linked question mentions hacking redis-py's connection pool. I really don't want to do this and have a forked version of redis-py (at least until the fix is hopefully accepted into master), but I've had a look at the redis-py code anyway and don't immediately see where this change would be made.

有人知道如何彻底解决悬挂的redis-py listen()调用吗?

Does anyone have an idea how to cleanly solve the hanging redis-py listen() call?

直接使用Thread._Thread__stop会导致什么问题?

What issues will I incur by directly using Thread._Thread__stop?

推荐答案

要关闭这么多年,才能解决这个问题.它最终是redis库中的一个bug.我对其进行了调试,并提交了 PR .它不应该再发生了.

To close this out after so many years. It ended up being a bug in the redis library. I debugged it and submitted the PR. It shouldn't occur any more.

这篇关于终止挂起的redis pubsub.listen()线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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