Note that there are some explanatory texts on larger screens.

plurals
  1. POTerminate a hung redis pubsub.listen() thread
    primarykey
    data
    text
    <p>Related to <a href="https://stackoverflow.com/questions/13654243/recover-from-dropped-connection-in-redis-pub-sub">this question</a> 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:</p> <pre><code>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 </code></pre> <p>In the linked question above, it is noted that <code>pubsub.listen()</code> never returns if the connection is dropped. Therefore, my <code>die()</code> function, while it can be called, will never actually cause the thread to terminate because it is hanging on the call to <code>listen()</code> inside the thread's <code>run()</code>.</p> <p>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. </p> <p>Does anyone have an idea how to cleanly solve the hanging redis-py <code>listen()</code> call? </p> <p>What issues will I incur by directly using <code>Thread._Thread__stop</code>?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload