Skip to content

Commit 9250644

Browse files
committed
use asyncios native timeout system
1 parent 2a70f19 commit 9250644

4 files changed

Lines changed: 29 additions & 28 deletions

File tree

poetry.lock

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

pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ branch = true
4646
[tool.poetry.dependencies]
4747
python = "^3.6"
4848
pyserial = "^3.4"
49-
async-timeout = "^3.0"
5049

5150
[tool.poetry.dev-dependencies]
5251
pytest = "^6.2"

src/scpi/scpi.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
import decimal
44
import re
55

6-
from async_timeout import timeout
7-
86
from .errors import CommandError
97
from .transports.baseclass import AbstractTransport
108

@@ -175,14 +173,19 @@ async def check_error(self, prev_command=""):
175173
async def command(self, command, cmd_timeout=COMMAND_DEFAULT_TIMEOUT, abort_on_timeout=True):
176174
"""Sends a command, does not wait for response"""
177175
try:
178-
with timeout(cmd_timeout):
176+
177+
async def _command() -> None:
178+
"""Wrap the actual work"""
179+
nonlocal self
179180
async with self.lock:
180181
await self.transport.send_command(command)
182+
183+
await asyncio.wait_for(_command(command), timeout=cmd_timeout)
181184
except (asyncio.TimeoutError, asyncio.CancelledError) as err:
182185
# check for the actual error if available
183186
await self.check_error(command)
184187
if abort_on_timeout:
185-
self.abort_command()
188+
await self.abort_command()
186189
# re-raise the timeout if no other error found
187190
raise err
188191
# other errors are allowed to bubble-up as-is
@@ -195,11 +198,14 @@ async def safe_command(self, command, *args, **kwargs):
195198
async def ask(self, command, cmd_timeout=COMMAND_DEFAULT_TIMEOUT, abort_on_timeout=True):
196199
"""Send a command and waits for response, returns the response"""
197200
try:
198-
with timeout(cmd_timeout):
201+
202+
async def _ask(command: str) -> str:
203+
"""Wrap the actual work"""
199204
async with self.lock:
200205
await self.transport.send_command(command)
201206
return await self.transport.get_response()
202207

208+
return await asyncio.wait_for(_ask(command), timeout=cmd_timeout)
203209
except (asyncio.TimeoutError, asyncio.CancelledError) as err:
204210
# check for the actual error if available
205211
await self.check_error(command)

src/scpi/transports/gpib/prologix.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
import serial
77
import serial.threaded
8-
from async_timeout import timeout
98

109
from ..rs232 import RS232SerialProtocol
1110
from .base import GPIBTransport
@@ -64,7 +63,9 @@ async def get_response(self):
6463
async def send_and_read(self, send):
6564
"""Send a line, read the response. NOTE: This is for talking with the controller, device responses
6665
need to use get_response as usual"""
67-
with timeout(READ_TIMEOUT):
66+
67+
async def _send_and_read(send) -> str:
68+
"""Wrap the actual work"""
6869
async with self.lock:
6970
response = None
7071

@@ -80,6 +81,8 @@ def set_response(message):
8081
await asyncio.get_event_loop().run_in_executor(None, self.blevent.wait)
8182
return response
8283

84+
return await asyncio.wait_for(_send_and_read(send), timeout=READ_TIMEOUT)
85+
8386
async def set_address(self, address, secondary=None):
8487
"""Set the address we want to talk to"""
8588
if secondary is None:
@@ -148,13 +151,18 @@ async def scan_devices(self):
148151
prev_read_tmo_ms = await self.send_and_read("++read_tmo_ms")
149152
self.serialhandler.protocol.write_line("++read_tmo_ms %d" % int((SCAN_DEVICE_TIMEOUT / 2) * 1000))
150153
for addr in range(0, 31): # 0-30 inclusive
151-
with timeout(SCAN_DEVICE_TIMEOUT):
152-
try:
153-
await self.set_address(addr)
154-
await self.poll()
155-
found_addresses.append(addr)
156-
except (asyncio.TimeoutError, asyncio.CancelledError):
157-
pass
154+
155+
async def _scan_addr(addr) -> None:
156+
"""Sacn single address"""
157+
nonlocal found_addresses
158+
await self.set_address(addr)
159+
await self.poll()
160+
found_addresses.append(addr)
161+
162+
try:
163+
await asyncio.wait_for(_scan_addr(addr), timeout=SCAN_DEVICE_TIMEOUT)
164+
except (asyncio.TimeoutError, asyncio.CancelledError):
165+
pass
158166
self.serialhandler.protocol.write_line("++read_tmo_ms " + prev_read_tmo_ms)
159167
# Wait a moment for things to settle
160168
await asyncio.sleep(float(prev_read_tmo_ms) / 1000)

0 commit comments

Comments
 (0)