|
1 | 1 | import asyncio
|
| 2 | +import contextlib |
2 | 3 | import copy
|
3 | 4 | import enum
|
4 | 5 | import errno
|
|
55 | 56 | if HIREDIS_AVAILABLE:
|
56 | 57 | import hiredis
|
57 | 58 |
|
| 59 | +if sys.version_info[:2] >= (3, 10): |
| 60 | + nullcontext = contextlib.nullcontext() |
| 61 | +else: |
| 62 | + |
| 63 | + class NullContext: |
| 64 | + async def __aenter__(self): |
| 65 | + pass |
| 66 | + |
| 67 | + async def __aexit__(self, *args): |
| 68 | + pass |
| 69 | + |
| 70 | + nullcontext = NullContext() |
| 71 | + |
58 | 72 | NONBLOCKING_EXCEPTION_ERROR_NUMBERS = {
|
59 | 73 | BlockingIOError: errno.EWOULDBLOCK,
|
60 | 74 | ssl.SSLWantReadError: 2,
|
@@ -922,19 +936,23 @@ async def can_read_destructive(self):
|
922 | 936 | f"Error while reading from {self.host}:{self.port}: {e.args}"
|
923 | 937 | )
|
924 | 938 |
|
925 |
| - async def read_response(self, disable_decoding: bool = False): |
| 939 | + async def read_response( |
| 940 | + self, |
| 941 | + disable_decoding: bool = False, |
| 942 | + timeout: Optional[float] = None, |
| 943 | + ): |
926 | 944 | """Read the response from a previously sent command"""
|
| 945 | + read_timeout = timeout if timeout is not None else self.socket_timeout |
927 | 946 | try:
|
928 |
| - if self.socket_timeout: |
929 |
| - async with async_timeout.timeout(self.socket_timeout): |
930 |
| - response = await self._parser.read_response( |
931 |
| - disable_decoding=disable_decoding |
932 |
| - ) |
933 |
| - else: |
| 947 | + async with async_timeout.timeout(read_timeout): |
934 | 948 | response = await self._parser.read_response(
|
935 | 949 | disable_decoding=disable_decoding
|
936 | 950 | )
|
937 | 951 | except asyncio.TimeoutError:
|
| 952 | + if timeout is not None: |
| 953 | + # user requested timeout, return None |
| 954 | + return None |
| 955 | + # it was a self.socket_timeout error. |
938 | 956 | await self.disconnect(nowait=True)
|
939 | 957 | raise TimeoutError(f"Timeout reading from {self.host}:{self.port}")
|
940 | 958 | except OSError as e:
|
|
0 commit comments