diff options
author | Kristján Valur Jónsson <sweskman@gmail.com> | 2022-05-02 09:25:34 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-05-02 12:25:34 +0300 |
commit | fdb9075745060e7a3633248fa6f419e895f010b7 (patch) | |
tree | d229abb20e27df2a4e2952b499ae056b595a749e /redis/asyncio/client.py | |
parent | 696d984a74ef6cd3e3968df8d11cf9af80057424 (diff) | |
download | redis-py-fdb9075745060e7a3633248fa6f419e895f010b7.tar.gz |
Async Connection: Allow `PubSub.run()` without previous `subscribe()` (#2148)
Diffstat (limited to 'redis/asyncio/client.py')
-rw-r--r-- | redis/asyncio/client.py | 15 |
1 files changed, 12 insertions, 3 deletions
diff --git a/redis/asyncio/client.py b/redis/asyncio/client.py index 7689e12..8dde96e 100644 --- a/redis/asyncio/client.py +++ b/redis/asyncio/client.py @@ -693,6 +693,15 @@ class PubSub: # legitimate message off the stack if the connection is already # subscribed to one or more channels + await self.connect() + connection = self.connection + kwargs = {"check_health": not self.subscribed} + await self._execute(connection, connection.send_command, *args, **kwargs) + + async def connect(self): + """ + Ensure that the PubSub is connected + """ if self.connection is None: self.connection = await self.connection_pool.get_connection( "pubsub", self.shard_hint @@ -700,9 +709,8 @@ class PubSub: # register a callback that re-subscribes to any channels we # were listening to when we were disconnected self.connection.register_connect_callback(self.on_connect) - connection = self.connection - kwargs = {"check_health": not self.subscribed} - await self._execute(connection, connection.send_command, *args, **kwargs) + else: + await self.connection.connect() async def _disconnect_raise_connect(self, conn, error): """ @@ -962,6 +970,7 @@ class PubSub: if handler is None: raise PubSubError(f"Pattern: '{pattern}' has no handler registered") + await self.connect() while True: try: await self.get_message( |