Skip to content

Commit

Permalink
add timeout to read_string
Browse files Browse the repository at this point in the history
  • Loading branch information
bmario committed Oct 14, 2024
1 parent a4b5bfa commit 7b6da9c
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions metricq_source_modbus/read_strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,18 @@ async def read_strings(
) -> StringReplacer:
if not strings:
return StringReplacer({})

logger.info("Reading device strings from {}:{}", host, port)
reader, writer = await asyncio.open_connection(host, port)
client = AsyncTCPClient((reader, writer))
values = {
key: await _read_string(client, slave_id, config)
for key, config in strings.items()
}
writer.close()
await writer.wait_closed()

async with asyncio.timeout(5):
reader, writer = await asyncio.open_connection(host, port)
client = AsyncTCPClient((reader, writer))
values = {
key: await _read_string(client, slave_id, config)
for key, config in strings.items()
}
writer.close()
await writer.wait_closed()

logger.info("Device strings for {}:{}: {}", host, port, values)
return StringReplacer(values)

0 comments on commit 7b6da9c

Please sign in to comment.