|
17 | 17 | from pprint import pformat as pf
|
18 | 18 | from typing import Dict, Generator, Optional, Union
|
19 | 19 |
|
| 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 | + |
20 | 24 | from .exceptions import SmartDeviceException
|
21 | 25 | from .json import dumps as json_dumps
|
22 | 26 | from .json import loads as json_loads
|
@@ -79,8 +83,10 @@ async def _connect(self, timeout: int) -> None:
|
79 | 83 | if self.writer:
|
80 | 84 | return
|
81 | 85 | self.reader = self.writer = None
|
| 86 | + |
82 | 87 | 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 |
84 | 90 |
|
85 | 91 | async def _execute_query(self, request: str) -> Dict:
|
86 | 92 | """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:
|
155 | 161 | try:
|
156 | 162 | assert self.reader is not None
|
157 | 163 | 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) |
161 | 166 | except Exception as ex:
|
162 | 167 | await self.close()
|
163 | 168 | if retry >= retry_count:
|
|
0 commit comments