@@ -4157,7 +4157,7 @@ def unsubscribe(self, *args) -> Awaitable:
41574157 async def listen (self ) -> AsyncIterator :
41584158 """Listen for messages on channels this client has been subscribed to"""
41594159 while self .subscribed :
4160- response = self .handle_message (await self .parse_response (block = True ))
4160+ response = await self .handle_message (await self .parse_response (block = True ))
41614161 if response is not None :
41624162 yield response
41634163
@@ -4173,7 +4173,7 @@ async def get_message(
41734173 """
41744174 response = await self .parse_response (block = False , timeout = timeout )
41754175 if response :
4176- return self .handle_message (response , ignore_subscribe_messages )
4176+ return await self .handle_message (response , ignore_subscribe_messages )
41774177 return None
41784178
41794179 def ping (self , message = None ) -> Awaitable :
@@ -4183,7 +4183,7 @@ def ping(self, message=None) -> Awaitable:
41834183 message = "" if message is None else message
41844184 return self .execute_command ("PING" , message )
41854185
4186- def handle_message (self , response , ignore_subscribe_messages = False ):
4186+ async def handle_message (self , response , ignore_subscribe_messages = False ):
41874187 """
41884188 Parses a pub/sub message. If the channel or pattern was subscribed to
41894189 with a message handler, the handler is invoked instead of a parsed
@@ -4232,7 +4232,10 @@ def handle_message(self, response, ignore_subscribe_messages=False):
42324232 else :
42334233 handler = self .channels .get (message ["channel" ], None )
42344234 if handler :
4235- handler (message )
4235+ if inspect .iscoroutinefunction (handler ):
4236+ await handler (message )
4237+ else :
4238+ handler (message )
42364239 return None
42374240 elif message_type != "pong" :
42384241 # this is a subscribe/unsubscribe message. ignore if we don't
0 commit comments