diff --git a/tangostationcontrol/tangostationcontrol/devices/interfaces/beam_device.py b/tangostationcontrol/tangostationcontrol/devices/interfaces/beam_device.py index 9744f00d7ef5bacf10f17d429924ea64125ff453..aae8de3cb787fcb3b364d88d544d0201216e0016 100644 --- a/tangostationcontrol/tangostationcontrol/devices/interfaces/beam_device.py +++ b/tangostationcontrol/tangostationcontrol/devices/interfaces/beam_device.py @@ -8,7 +8,6 @@ from json import loads from statistics import median -# from threading import Thread, Lock, Condition import asyncio from typing import Callable import datetime @@ -547,7 +546,7 @@ class BeamTracker: """""" self.done = False - self.thread = None + self.loop = None self.interval = interval self.preparation_time = preparation_time @@ -556,8 +555,6 @@ class BeamTracker: self.fault_callback = fault_callback # Condition to trigger a forced update or early abort - # self.update_lock = Lock() - # self.update_condition = Condition(self.update_lock) self.update_lock = asyncio.Lock() self.update_condition = asyncio.Condition(self.update_lock) @@ -571,20 +568,18 @@ class BeamTracker: async def start(self): """Starts the Beam Tracking thread""" - if self.thread: + if self.loop: # already started return - # self.thread = Thread(target=self._update_pointing_direction, name="BeamTracker") - # self.thread.start() - loop = asyncio.get_event_loop() - loop.create_task(self._update_pointing_direction()) + self.loop = asyncio.get_event_loop() + self.loop.create_task(self._update_pointing_direction()) logger.info("[BeamTracking] Thread started") async def is_alive(self) -> bool: """True just before and after run method starts and terminates""" - return self.thread and self.thread.is_alive() + return self.loop and (self._update_pointing_direction() in asyncio.all_tasks()) async def force_update(self): """Force the pointing to be updated.""" @@ -600,21 +595,21 @@ class BeamTracker: async def stop(self): """Stops the Beam Tracking loop""" - if not self.thread: + if not self.loop: return logger.info("[BeamTracking] Thread stopping") self.done = True - self.force_update() + await self.force_update() # wait for thread to finish - self.thread.join(self.DISCONNECT_TIMEOUT) + self.loop.run_until_complete(self._update_pointing_direction()) if self.is_alive(): logger.error("[BeamTracking] Thread did not properly terminate") - self.thread = None + self.loop = None logger.info("[BeamTracking] Thread stopped")