encode / broadcaster

Broadcast channels for async web apps. 📢
BSD 3-Clause "New" or "Revised" License
1.13k stars 121 forks source link

redis-BackEnd listener just listen once #133

Closed Fly-Playgroud closed 4 months ago

Fly-Playgroud commented 4 months ago

https://github.com/encode/broadcaster/blob/22e8b2afb131321dcd0a9a06be2d860849cfbe5a/broadcaster/_backends/redis.py#L43-L53

the redis-backend pub-listener only just listen once, if channels unsubscribed until here no channels to subscribe, this async.Task will exited. Even if new subscriptions come in later, no messages can be listened because of it has eixted.

Fly-Playgroud commented 4 months ago

I think maybe use while loop to keep running

Fly-Playgroud commented 4 months ago

solve code:

 async def _pubsub_listener(self) -> None: 
     # redis-py does not listen to the pubsub connection if there are no channels subscribed 
     # so we need to wait until the first channel is subscribed to start listening
     while True:
         await self._ready.wait() 
         async for message in self._pubsub.listen(): 
             if message["type"] == "message": 
                 event = Event( 
                     channel=message["channel"].decode(), 
                     message=message["data"].decode(), 
                 ) 
                 await self._queue.put(event) 
        self._ready.clear()
alex-oleshkevich commented 4 months ago

This looks like a great solution! Can you make a PR?

Fly-Playgroud commented 4 months ago

This looks like a great solution! Can you make a PR?

ok, I will do it

alex-oleshkevich commented 4 months ago

merged,