From b69940afef0939510d10b39ffa9c24f613a3a003 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20S=C3=A1nchez-Gallego?= Date: Sat, 2 Nov 2024 18:27:01 +0000 Subject: [PATCH] Add a lock for the status command --- CHANGELOG.md | 4 ++++ python/lvmecp/actor/actor.py | 2 ++ python/lvmecp/actor/commands/status.py | 21 +++++++++++---------- 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 557abd1..ec4c7d0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## Next version +### ✨ Improved + +* Add a lock for the status command to prevent multiple concurrent requests. + ### ⚙️ Engineering Use `uv` for packaging. diff --git a/python/lvmecp/actor/actor.py b/python/lvmecp/actor/actor.py index f9e9a14..9bcc365 100644 --- a/python/lvmecp/actor/actor.py +++ b/python/lvmecp/actor/actor.py @@ -56,6 +56,8 @@ def __init__( else: self.plc = plc + self.lock = asyncio.Lock() + async def start(self, **kwargs): """Starts the actor.""" diff --git a/python/lvmecp/actor/commands/status.py b/python/lvmecp/actor/commands/status.py index d1fb2a6..a7aeafc 100644 --- a/python/lvmecp/actor/commands/status.py +++ b/python/lvmecp/actor/commands/status.py @@ -29,16 +29,17 @@ async def status(command: ECPCommand, no_registers: bool = False): plc = command.actor.plc - if no_registers is False: - command.info(registers=(await plc.read_all_registers(use_cache=False))) - - modules: list[PLCModule] = [plc.dome, plc.safety, plc.lights] - await asyncio.gather( - *[ - module.update(force_output=True, command=command, use_cache=True) - for module in modules - ] - ) + async with command.actor.lock: + if no_registers is False: + command.info(registers=(await plc.read_all_registers(use_cache=False))) + + modules: list[PLCModule] = [plc.dome, plc.safety, plc.lights] + await asyncio.gather( + *[ + module.update(force_output=True, command=command, use_cache=True) + for module in modules + ] + ) command.info( o2_percent_utilities=plc.safety.o2_level_utilities,