mirror of
https://github.com/python-kasa/python-kasa.git
synced 2024-12-22 19:23:34 +00:00
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 https://github.com/python/cpython/pull/98518
This commit is contained in:
parent
c9faa1f78f
commit
117a7ac64a
@ -17,6 +17,10 @@ import struct
|
|||||||
from pprint import pformat as pf
|
from pprint import pformat as pf
|
||||||
from typing import Dict, Generator, Optional, Union
|
from typing import Dict, Generator, Optional, Union
|
||||||
|
|
||||||
|
# When support for cpython older than 3.11 is dropped
|
||||||
|
# async_timeout can be replaced with asyncio.timeout
|
||||||
|
from async_timeout import timeout as asyncio_timeout
|
||||||
|
|
||||||
from .exceptions import SmartDeviceException
|
from .exceptions import SmartDeviceException
|
||||||
from .json import dumps as json_dumps
|
from .json import dumps as json_dumps
|
||||||
from .json import loads as json_loads
|
from .json import loads as json_loads
|
||||||
@ -79,8 +83,10 @@ class TPLinkSmartHomeProtocol:
|
|||||||
if self.writer:
|
if self.writer:
|
||||||
return
|
return
|
||||||
self.reader = self.writer = None
|
self.reader = self.writer = None
|
||||||
|
|
||||||
task = asyncio.open_connection(self.host, self.port)
|
task = asyncio.open_connection(self.host, self.port)
|
||||||
self.reader, self.writer = await asyncio.wait_for(task, timeout=timeout)
|
async with asyncio_timeout(timeout):
|
||||||
|
self.reader, self.writer = await task
|
||||||
|
|
||||||
async def _execute_query(self, request: str) -> Dict:
|
async def _execute_query(self, request: str) -> Dict:
|
||||||
"""Execute a query on the device and wait for the response."""
|
"""Execute a query on the device and wait for the response."""
|
||||||
@ -155,9 +161,8 @@ class TPLinkSmartHomeProtocol:
|
|||||||
try:
|
try:
|
||||||
assert self.reader is not None
|
assert self.reader is not None
|
||||||
assert self.writer is not None
|
assert self.writer is not None
|
||||||
return await asyncio.wait_for(
|
async with asyncio_timeout(timeout):
|
||||||
self._execute_query(request), timeout=timeout
|
return await self._execute_query(request)
|
||||||
)
|
|
||||||
except Exception as ex:
|
except Exception as ex:
|
||||||
await self.close()
|
await self.close()
|
||||||
if retry >= retry_count:
|
if retry >= retry_count:
|
||||||
|
14
poetry.lock
generated
14
poetry.lock
generated
@ -34,6 +34,18 @@ doc = ["Sphinx (>=6.1.0)", "packaging", "sphinx-autodoc-typehints (>=1.2.0)", "s
|
|||||||
test = ["anyio[trio]", "coverage[toml] (>=4.5)", "hypothesis (>=4.0)", "mock (>=4)", "psutil (>=5.9)", "pytest (>=7.0)", "pytest-mock (>=3.6.1)", "trustme", "uvloop (>=0.17)"]
|
test = ["anyio[trio]", "coverage[toml] (>=4.5)", "hypothesis (>=4.0)", "mock (>=4)", "psutil (>=5.9)", "pytest (>=7.0)", "pytest-mock (>=3.6.1)", "trustme", "uvloop (>=0.17)"]
|
||||||
trio = ["trio (<0.22)"]
|
trio = ["trio (<0.22)"]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "async-timeout"
|
||||||
|
version = "4.0.2"
|
||||||
|
description = "Timeout context manager for asyncio programs"
|
||||||
|
category = "main"
|
||||||
|
optional = false
|
||||||
|
python-versions = ">=3.6"
|
||||||
|
files = [
|
||||||
|
{file = "async-timeout-4.0.2.tar.gz", hash = "sha256:2163e1640ddb52b7a8c80d0a67a08587e5d245cc9c553a74a847056bc2976b15"},
|
||||||
|
{file = "async_timeout-4.0.2-py3-none-any.whl", hash = "sha256:8ca1e4fcf50d07413d66d1a5e416e42cfdf5851c981d679a09851a6853383b3c"},
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "asyncclick"
|
name = "asyncclick"
|
||||||
version = "8.1.3.4"
|
version = "8.1.3.4"
|
||||||
@ -1404,4 +1416,4 @@ speedups = ["orjson", "kasa-crypt"]
|
|||||||
[metadata]
|
[metadata]
|
||||||
lock-version = "2.0"
|
lock-version = "2.0"
|
||||||
python-versions = "^3.8"
|
python-versions = "^3.8"
|
||||||
content-hash = "b85f55f0ca928b1f3510da37196c21f40eb07cd4d07b3a9c3dd29215ba9777fe"
|
content-hash = "fcb657fbabe28548021f5f6a1fcb7b60aa82d60f3de015b4b0c7b37260a6a29f"
|
||||||
|
@ -34,6 +34,7 @@ sphinx_rtd_theme = { version = "^0", optional = true }
|
|||||||
sphinxcontrib-programoutput = { version = "^0", optional = true }
|
sphinxcontrib-programoutput = { version = "^0", optional = true }
|
||||||
myst-parser = { version = "*", optional = true }
|
myst-parser = { version = "*", optional = true }
|
||||||
docutils = { version = ">=0.17", optional = true }
|
docutils = { version = ">=0.17", optional = true }
|
||||||
|
async-timeout = ">=3.0.0"
|
||||||
|
|
||||||
[tool.poetry.dev-dependencies]
|
[tool.poetry.dev-dependencies]
|
||||||
pytest = "*"
|
pytest = "*"
|
||||||
|
Loading…
Reference in New Issue
Block a user