forked from M-Labs/thermostat
Shield pending commands from cancellation
This commit is contained in:
parent
39a78b92c4
commit
7149fb6d85
|
@ -74,12 +74,16 @@ class Client:
|
||||||
chunk = await self._reader.readline()
|
chunk = await self._reader.readline()
|
||||||
return chunk.decode('utf-8', errors='ignore')
|
return chunk.decode('utf-8', errors='ignore')
|
||||||
|
|
||||||
async def _command(self, *command):
|
async def _read_write(self, command):
|
||||||
async with self._command_lock:
|
|
||||||
self._writer.write(((" ".join(command)).strip() + "\n").encode('utf-8'))
|
self._writer.write(((" ".join(command)).strip() + "\n").encode('utf-8'))
|
||||||
await self._writer.drain()
|
await self._writer.drain()
|
||||||
|
|
||||||
line = await self._read_line()
|
return await self._read_line()
|
||||||
|
|
||||||
|
async def _command(self, *command):
|
||||||
|
async with self._command_lock:
|
||||||
|
# protect the read-write process from being cancelled midway
|
||||||
|
line = await asyncio.shield(self._read_write(command))
|
||||||
|
|
||||||
response = json.loads(line)
|
response = json.loads(line)
|
||||||
logging.debug(f"{command}: {response}")
|
logging.debug(f"{command}: {response}")
|
||||||
|
|
Loading…
Reference in New Issue