FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Replace asyncio.wait_for with async-timeout by bdraco · Pull Request #480 · python-kasa/python-kasa · GitHub

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .lock  (1) .py  (1) .toml  (1) All 3 file types selected
Only manifest files
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
13 changes: 9 additions & 4 deletions kasa/protocol.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
from pprint import pformat as pf
from typing import Dict, Generator, Optional, Union

# When support for cpython older than 3.11 is dropped
Comment thread
bdraco marked this conversation as resolved.
# async_timeout can be replaced with asyncio.timeout
from async_timeout import timeout as asyncio_timeout

from .exceptions import SmartDeviceException
from .json import dumps as json_dumps
from .json import loads as json_loads
Expand Down Expand Up @@ -79,8 +83,10 @@ async def _connect(self, timeout: int) -> None:
if self.writer:
return
self.reader = self.writer = None

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:
"""Execute a query on the device and wait for the response."""
Expand Down Expand Up @@ -155,9 +161,8 @@ async def _query(self, request: str, retry_count: int, timeout: int) -> Dict:
try:
assert self.reader is not None
assert self.writer is not None
return await asyncio.wait_for(
self._execute_query(request), timeout=timeout
)
async with asyncio_timeout(timeout):
return await self._execute_query(request)
except Exception as ex:
await self.close()
if retry >= retry_count:
Expand Down
14 changes: 13 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ sphinx_rtd_theme = { version = "^0", optional = true }
sphinxcontrib-programoutput = { version = "^0", optional = true }
myst-parser = { version = "*", optional = true }
docutils = { version = ">=0.17", optional = true }
async-timeout = ">=3.0.0"

[tool.poetry.dev-dependencies]
pytest = "*"
Expand Down

Back | FazBrowse Home | New Git URL