Skip to content

Commit

Permalink
fix: solve the redisBackend _pubsub_listener just listen once.
Browse files Browse the repository at this point in the history
  • Loading branch information
Fly-Playgroud committed Jul 29, 2024
1 parent 22e8b2a commit 865afc3
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions broadcaster/_backends/redis.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,20 @@ async def next_published(self) -> Event:
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
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)
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)

# when no channel subscribed, clear the event.
# And then in next loop, event will blocked again until
# the new channel subscribed.Now asyncio.Task exited again.
self._ready.clear()


StreamMessageType = typing.Tuple[bytes, typing.Tuple[typing.Tuple[bytes, typing.Dict[bytes, bytes]]]]
Expand Down

0 comments on commit 865afc3

Please sign in to comment.