Read the response from a previously sent command. ``timeout`` semantics: - ``None`` (default): fall back to ``self.socket_timeout``. - ``math.inf``: block indefinitely with no timeout. Used by PubSub blocking reads (``listen()`` / ``get_message(timeout=None)`` /
(
self,
disable_decoding: bool = False,
timeout: Optional[float] = None,
*,
disconnect_on_error: bool = True,
push_request: Optional[bool] = False,
)
| 740 | raise ConnectionError(f"Error while reading from {host_error}: {e.args}") |
| 741 | |
| 742 | async def read_response( |
| 743 | self, |
| 744 | disable_decoding: bool = False, |
| 745 | timeout: Optional[float] = None, |
| 746 | *, |
| 747 | disconnect_on_error: bool = True, |
| 748 | push_request: Optional[bool] = False, |
| 749 | ): |
| 750 | """Read the response from a previously sent command. |
| 751 | |
| 752 | ``timeout`` semantics: |
| 753 | - ``None`` (default): fall back to ``self.socket_timeout``. |
| 754 | - ``math.inf``: block indefinitely with no timeout. Used by PubSub |
| 755 | blocking reads (``listen()`` / ``get_message(timeout=None)`` / |
| 756 | ``parse_response(block=True)``) where the configured |
| 757 | ``socket_timeout`` must not abort the read. |
| 758 | - ``float``: apply that timeout in seconds for this single read. |
| 759 | |
| 760 | TODO(next-major): replace the ``math.inf`` opt-in with a SENTINEL |
| 761 | default for ``timeout``. After that change, ``timeout=None`` will |
| 762 | mean "no timeout, block until a response arrives" (matching the |
| 763 | long-standing PubSub docstring contract) and the SENTINEL default |
| 764 | will be the value that falls back to ``self.socket_timeout``. |
| 765 | That swap is a breaking change, so it must wait for a major |
| 766 | release. Until then, callers that need an indefinitely blocking |
| 767 | read pass ``math.inf`` explicitly. |
| 768 | """ |
| 769 | # TODO(next-major): drop the math.inf branch. Use SENTINEL as the |
| 770 | # default for ``timeout`` and treat ``timeout is None`` as the |
| 771 | # "no timeout" signal (matching the PubSub docstring contract). |
| 772 | # Match only positive infinity here. ``-math.inf`` is not a valid |
| 773 | # "block forever" signal and historically behaved as an already- |
| 774 | # expired timeout; preserve that. |
| 775 | if timeout == math.inf: |
| 776 | read_timeout = None |
| 777 | else: |
| 778 | read_timeout = timeout if timeout is not None else self.socket_timeout |
| 779 | host_error = self._host_error() |
| 780 | try: |
| 781 | if read_timeout is not None and self.protocol in ["3", 3]: |
| 782 | async with async_timeout(read_timeout): |
| 783 | response = await self._parser.read_response( |
| 784 | disable_decoding=disable_decoding, push_request=push_request |
| 785 | ) |
| 786 | elif read_timeout is not None: |
| 787 | async with async_timeout(read_timeout): |
| 788 | response = await self._parser.read_response( |
| 789 | disable_decoding=disable_decoding |
| 790 | ) |
| 791 | elif self.protocol in ["3", 3]: |
| 792 | response = await self._parser.read_response( |
| 793 | disable_decoding=disable_decoding, push_request=push_request |
| 794 | ) |
| 795 | else: |
| 796 | response = await self._parser.read_response( |
| 797 | disable_decoding=disable_decoding |
| 798 | ) |
| 799 | except asyncio.TimeoutError: |
no test coverage detected