Skip to content

How to handle connection interruption/timeout in PubSub listen() ? #386

@janeczku

Description

@janeczku

What happens when connection to Redis goes down or timeouts while listening to PubSub messages via a long running "pubsub.listen()" ?
Any tips how i can gracefully handle / restart interrupted connection?
I do appreciate your help!

This is the relevant code:

class Listener(threading.Thread):
    def __init__(self, r, channels):
        threading.Thread.__init__(self)
        self.redis = r
        self.pubsub = self.redis.pubsub()
        try:
            self.pubsub.subscribe(channels)
        except ConnectionError:
            print self, "Redis is down"

    def work(self, item):
        print item['channel'], ":", item['data']

    def run(self):
        for item in self.pubsub.listen():
            if item['data'] == "KILL":
                self.pubsub.unsubscribe()
                print self, "unsubscribed and finished"
                break
            else:
                self.work(item)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions