Get the next message if one is available, otherwise None. If timeout is specified, the system will wait for `timeout` seconds before returning. Timeout should be specified as a floating point number, or None, to wait indefinitely.
(
self, ignore_subscribe_messages: bool = False, timeout: float = 0.0
)
| 1451 | yield response |
| 1452 | |
| 1453 | def get_message( |
| 1454 | self, ignore_subscribe_messages: bool = False, timeout: float = 0.0 |
| 1455 | ): |
| 1456 | """ |
| 1457 | Get the next message if one is available, otherwise None. |
| 1458 | |
| 1459 | If timeout is specified, the system will wait for `timeout` seconds |
| 1460 | before returning. Timeout should be specified as a floating point |
| 1461 | number, or None, to wait indefinitely. |
| 1462 | """ |
| 1463 | if not self.subscribed: |
| 1464 | # Wait for subscription |
| 1465 | start_time = time.monotonic() |
| 1466 | if self.subscribed_event.wait(timeout) is True: |
| 1467 | # The connection was subscribed during the timeout time frame. |
| 1468 | # The timeout should be adjusted based on the time spent |
| 1469 | # waiting for the subscription |
| 1470 | time_spent = time.monotonic() - start_time |
| 1471 | timeout = max(0.0, timeout - time_spent) |
| 1472 | else: |
| 1473 | # The connection isn't subscribed to any channels or patterns, |
| 1474 | # so no messages are available |
| 1475 | return None |
| 1476 | |
| 1477 | response = self.parse_response(block=(timeout is None), timeout=timeout) |
| 1478 | |
| 1479 | if response: |
| 1480 | return self.handle_message(response, ignore_subscribe_messages) |
| 1481 | return None |
| 1482 | |
| 1483 | get_sharded_message = get_message |
| 1484 |