Skip to content

Commit 117a7ac

Browse files
authored
Replace asyncio.wait_for with async-timeout (#480)
asyncio.wait_for has some underlying problems that are only fixed in cpython 3.12. Use async_timeout instead until the minimum supported version is 3.11+ and it can be replaced with asyncio.timeout See python/cpython#98518
1 parent c9faa1f commit 117a7ac

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

kasa/protocol.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717
from pprint import pformat as pf
1818
from typing import Dict, Generator, Optional, Union
1919

20+
# When support for cpython older than 3.11 is dropped
21+
# async_timeout can be replaced with asyncio.timeout
22+
from async_timeout import timeout as asyncio_timeout
23+
2024
from .exceptions import SmartDeviceException
2125
from .json import dumps as json_dumps
2226
from .json import loads as json_loads
@@ -79,8 +83,10 @@ async def _connect(self, timeout: int) -> None:
7983
if self.writer:
8084
return
8185
self.reader = self.writer = None
86+
8287
task = asyncio.open_connection(self.host, self.port)
83-
self.reader, self.writer = await asyncio.wait_for(task, timeout=timeout)
88+
async with asyncio_timeout(timeout):
89+
self.reader, self.writer = await task
8490

8591
async def _execute_query(self, request: str) -> Dict:
8692
"""Execute a query on the device and wait for the response."""
@@ -155,9 +161,8 @@ async def _query(self, request: str, retry_count: int, timeout: int) -> Dict:
155161
try:
156162
assert self.reader is not None
157163
assert self.writer is not None
158-
return await asyncio.wait_for(
159-
self._execute_query(request), timeout=timeout
160-
)
164+
async with asyncio_timeout(timeout):
165+
return await self._execute_query(request)
161166
except Exception as ex:
162167
await self.close()
163168
if retry >= retry_count:

poetry.lock

Lines changed: 13 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ sphinx_rtd_theme = { version = "^0", optional = true }
3434
sphinxcontrib-programoutput = { version = "^0", optional = true }
3535
myst-parser = { version = "*", optional = true }
3636
docutils = { version = ">=0.17", optional = true }
37+
async-timeout = ">=3.0.0"
3738

3839
[tool.poetry.dev-dependencies]
3940
pytest = "*"

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy