Parse the response from a publish/subscribe command. Args: block: If True, block indefinitely until a message is available. If False, return immediately if no message is available. Default: True timeout: The timeout in s
(self, block=True, timeout=0)
| 1201 | raise |
| 1202 | |
| 1203 | def parse_response(self, block=True, timeout=0): |
| 1204 | """ |
| 1205 | Parse the response from a publish/subscribe command. |
| 1206 | |
| 1207 | Args: |
| 1208 | block: If True, block indefinitely until a message is available. |
| 1209 | If False, return immediately if no message is available. |
| 1210 | Default: True |
| 1211 | timeout: The timeout in seconds for reading a response when block=False. |
| 1212 | This parameter is ignored when block=True. |
| 1213 | Default: 0 (return immediately if no data available) |
| 1214 | |
| 1215 | Returns: |
| 1216 | The parsed response from the server, or None if no message is available |
| 1217 | within the timeout period (when block=False). |
| 1218 | |
| 1219 | Important: |
| 1220 | The block and timeout parameters work together: |
| 1221 | - When block=True: timeout is IGNORED, method blocks indefinitely |
| 1222 | - When block=False: timeout is USED, method returns after timeout expires |
| 1223 | |
| 1224 | Typically, you should use get_message(timeout=X) instead of calling |
| 1225 | parse_response() directly. The get_message() method automatically sets |
| 1226 | block=False when a timeout is provided, and block=True when timeout=None. |
| 1227 | |
| 1228 | Example: |
| 1229 | # Block indefinitely (timeout is ignored) |
| 1230 | response = pubsub.parse_response(block=True, timeout=0.1) |
| 1231 | |
| 1232 | # Non-blocking with 0.1 second timeout |
| 1233 | response = pubsub.parse_response(block=False, timeout=0.1) |
| 1234 | |
| 1235 | # Non-blocking, return immediately |
| 1236 | response = pubsub.parse_response(block=False, timeout=0) |
| 1237 | |
| 1238 | # Recommended: use get_message() instead |
| 1239 | msg = pubsub.get_message(timeout=0.1) # automatically sets block=False |
| 1240 | msg = pubsub.get_message(timeout=None) # automatically sets block=True |
| 1241 | """ |
| 1242 | conn = self.connection |
| 1243 | if conn is None: |
| 1244 | raise RuntimeError( |
| 1245 | "pubsub connection not set: " |
| 1246 | "did you forget to call subscribe() or psubscribe()?" |
| 1247 | ) |
| 1248 | |
| 1249 | self.check_health() |
| 1250 | |
| 1251 | def try_read(): |
| 1252 | if not block: |
| 1253 | if not conn.can_read(timeout=timeout): |
| 1254 | return None |
| 1255 | read_timeout = timeout |
| 1256 | else: |
| 1257 | conn.connect() |
| 1258 | # Block indefinitely waiting for a pubsub message. timeout=None |
| 1259 | # makes the socket layer call sock.settimeout(None) for this read |
| 1260 | # (and restore the original socket_timeout afterwards), so the |
no test coverage detected