diff options
author | Kristján Valur Jónsson <sweskman@gmail.com> | 2022-09-29 13:37:55 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-09-29 16:37:55 +0300 |
commit | b0883b791f95a595fae70bcedf3ad0f73c00e258 (patch) | |
tree | 6bfeb975aa6b9ec6325e90246230d01172beb1a6 /redis/asyncio/client.py | |
parent | cdbc662adcd303d2525f3ace70531aa37a755652 (diff) | |
download | redis-py-b0883b791f95a595fae70bcedf3ad0f73c00e258.tar.gz |
Simplify async timeouts and allowing `timeout=None` in `PubSub.get_message()` to wait forever (#2295)v4.4.0rc2
* Avoid an extra "can_read" call and use timeout directly.
* Remove low-level read timeouts from the Parser, now handled in the Connection
* Allow pubsub.get_message(time=None) to block.
* update Changes
* increase test timeout for robustness
* expand with statement to avoid invoking null context managers.
remove nullcontext
* Remove unused import
Diffstat (limited to 'redis/asyncio/client.py')
-rw-r--r-- | redis/asyncio/client.py | 22 |
1 files changed, 5 insertions, 17 deletions
diff --git a/redis/asyncio/client.py b/redis/asyncio/client.py index c13054b..0e40ed7 100644 --- a/redis/asyncio/client.py +++ b/redis/asyncio/client.py @@ -24,8 +24,6 @@ from typing import ( cast, ) -import async_timeout - from redis.asyncio.connection import ( Connection, ConnectionPool, @@ -759,18 +757,8 @@ class PubSub: if not conn.is_connected: await conn.connect() - if not block: - - async def read_with_timeout(): - try: - async with async_timeout.timeout(timeout): - return await conn.read_response() - except asyncio.TimeoutError: - return None - - response = await self._execute(conn, read_with_timeout) - else: - response = await self._execute(conn, conn.read_response) + read_timeout = None if block else timeout + response = await self._execute(conn, conn.read_response, timeout=read_timeout) if conn.health_check_interval and response == self.health_check_response: # ignore the health check message as user might not expect it @@ -882,16 +870,16 @@ class PubSub: yield response async def get_message( - self, ignore_subscribe_messages: bool = False, timeout: float = 0.0 + self, ignore_subscribe_messages: bool = False, timeout: Optional[float] = 0.0 ): """ 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. + number or None to wait indefinitely. """ - response = await self.parse_response(block=False, timeout=timeout) + response = await self.parse_response(block=(timeout is None), timeout=timeout) if response: return await self.handle_message(response, ignore_subscribe_messages) return None |