question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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)

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Reactions:1
  • Comments:12 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
pmdscullycommented, Aug 21, 2019

Hi @andymccurdy and all, I realise the issue is closed, but I still experienced the issue. I would like to recommend a readme update to include explicit shutdown order for removal of the thread interruption error with pubsub, mentioned. For me, this has removed exceptions errors caused by interruption of the thread created in run_in_thread() during application shutdown.

This for the use case as follows:

    p = r.pubsub()
    p.subscribe( **{key: handler_func} )
    t = p.run_in_thread(sleep_time=0.1)

then explicit order for shutdown is:

    t.stop()
    t.join()
    p.close()

Optionally wrapped in try/excepts.

I’m using PyPi release v3.3.8.

Hopefully that will help someone in future with the same issue. Thanks,

1reaction
srimancommented, Apr 28, 2018

I have faced this problem mainly because I am using elastic cache of aws it tends to close the connection sometimes, no specific usecase when it happens, in the python code whenever connection closes its not reconnected again. thats why subscriber can’t listen to the signal to the subscribe, to solve this there is option called socket keep alive in this library, give a shot with that option

On Sat 28 Apr, 2018, 12:48 PM yuzhushang, notifications@github.com wrote:

hi @sriman https://github.com/sriman

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/andymccurdy/redis-py/issues/386#issuecomment-385148175, or mute the thread https://github.com/notifications/unsubscribe-auth/ACLmXS54h7W6Z4zRnEnJE30xwfkV3as2ks5ttBevgaJpZM4BEVVc .

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to handle connection interruption/timeout in PubSub listen()
listen() " ? Any tips how i can gracefully handle / restart interrupted connection? I do appreciate your help! This is the relevant...
Read more >
Terminate a hung redis pubsub.listen() thread - Stack Overflow
listen() never returns if the connection is dropped. Therefore, my die() function, while it can be called, will never actually cause the thread ......
Read more >
Reliable PUBSUB and Blocking List Operations in an HA ...
On a failover the client code is expected to detect the connection has failed and either a) look up the current master and...
Read more >
pubsub listen with a timeout? - Google Groups
It would be useful, yes. On the other hand, if you send a heartbeat down your pubsub channels, you can implement timeouts on...
Read more >
redis-py · master · Sampa / Redis · GitLab
Behind the scenes, redis-py uses a connection pool to manage connections to a Redis ... Older versions of redis-py only read messages with...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found