@@ -693,16 +693,24 @@ async def execute_command(self, *args: EncodableT):
693
693
# legitimate message off the stack if the connection is already
694
694
# subscribed to one or more channels
695
695
696
+ await self .connect ()
697
+ connection = self .connection
698
+ kwargs = {"check_health" : not self .subscribed }
699
+ await self ._execute (connection , connection .send_command , * args , ** kwargs )
700
+
701
+ async def connect (self ):
702
+ """
703
+ Ensure that the PubSub is connected
704
+ """
696
705
if self .connection is None :
697
706
self .connection = await self .connection_pool .get_connection (
698
707
"pubsub" , self .shard_hint
699
708
)
700
709
# register a callback that re-subscribes to any channels we
701
710
# were listening to when we were disconnected
702
711
self .connection .register_connect_callback (self .on_connect )
703
- connection = self .connection
704
- kwargs = {"check_health" : not self .subscribed }
705
- await self ._execute (connection , connection .send_command , * args , ** kwargs )
712
+ else :
713
+ await self .connection .connect ()
706
714
707
715
async def _disconnect_raise_connect (self , conn , error ):
708
716
"""
@@ -962,6 +970,7 @@ async def run(
962
970
if handler is None :
963
971
raise PubSubError (f"Pattern: '{ pattern } ' has no handler registered" )
964
972
973
+ await self .connect ()
965
974
while True :
966
975
try :
967
976
await self .get_message (
0 commit comments