How to handle connection interruption/timeout in PubSub listen() ?
See original GitHub issueWhat 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:
- Created 10 years ago
- Reactions:1
- Comments:12 (2 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

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:
then explicit order for shutdown is:
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,
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: