mirror of
https://github.com/bec-project/bec_widgets.git
synced 2025-07-14 03:31:50 +02:00
fix(cli): added gui heartbeat
This commit is contained in:
@ -3,6 +3,7 @@ from __future__ import annotations
|
||||
import importlib
|
||||
import select
|
||||
import subprocess
|
||||
import time
|
||||
import uuid
|
||||
from functools import wraps
|
||||
from typing import TYPE_CHECKING
|
||||
@ -32,6 +33,8 @@ def rpc_call(func):
|
||||
|
||||
@wraps(func)
|
||||
def wrapper(self, *args, **kwargs):
|
||||
if not self.gui_is_alive():
|
||||
raise RuntimeError("GUI is not alive")
|
||||
return self._run_rpc(func.__name__, *args, **kwargs)
|
||||
|
||||
return wrapper
|
||||
@ -135,6 +138,9 @@ class BECFigureClientMixin:
|
||||
"""
|
||||
if self._process is None or self._process.poll() is not None:
|
||||
self._start_plot_process()
|
||||
while not self.gui_is_alive():
|
||||
print("Waiting for GUI to start...")
|
||||
time.sleep(0.5)
|
||||
|
||||
def close(self) -> None:
|
||||
"""
|
||||
@ -266,9 +272,16 @@ class RPCBase:
|
||||
Wait for the response from the server.
|
||||
"""
|
||||
response = None
|
||||
while response is None:
|
||||
while response is None and self.gui_is_alive():
|
||||
response = self._client.connector.get(
|
||||
MessageEndpoints.gui_instruction_response(request_id)
|
||||
)
|
||||
QCoreApplication.processEvents() # keep UI responsive (and execute signals/slots)
|
||||
return response
|
||||
|
||||
def gui_is_alive(self):
|
||||
"""
|
||||
Check if the GUI is alive.
|
||||
"""
|
||||
heart = self._client.connector.get(MessageEndpoints.gui_heartbeat(self._root._gui_id))
|
||||
return heart is not None
|
||||
|
@ -1,11 +1,13 @@
|
||||
import inspect
|
||||
import threading
|
||||
import time
|
||||
|
||||
from bec_lib import MessageEndpoints, messages
|
||||
|
||||
from bec_widgets.utils import BECDispatcher
|
||||
from bec_widgets.utils.bec_connector import BECConnector
|
||||
from bec_widgets.widgets.figure import BECFigure
|
||||
from bec_widgets.widgets.plots import BECCurve, BECWaveform1D, BECImageShow
|
||||
from bec_widgets.widgets.plots import BECCurve, BECImageShow, BECWaveform1D
|
||||
|
||||
|
||||
class BECWidgetsCLIServer:
|
||||
@ -18,10 +20,12 @@ class BECWidgetsCLIServer:
|
||||
self.gui_id = gui_id
|
||||
self.fig = BECFigure(gui_id=self.gui_id)
|
||||
# print(f"Server started with gui_id {self.gui_id}")
|
||||
|
||||
self._shutdown_event = threading.Event()
|
||||
self.dispatcher.connect_slot(
|
||||
self.on_rpc_update, MessageEndpoints.gui_instructions(self.gui_id)
|
||||
)
|
||||
self._heartbeat = threading.Thread(target=self.start_heartbeat, daemon=True)
|
||||
self._heartbeat.start()
|
||||
|
||||
def start(self):
|
||||
"""Start the figure window."""
|
||||
@ -95,10 +99,20 @@ class BECWidgetsCLIServer:
|
||||
}
|
||||
return obj
|
||||
|
||||
def start_heartbeat(self):
|
||||
while not self._shutdown_event.is_set():
|
||||
self.client.connector.set(
|
||||
MessageEndpoints.gui_heartbeat(self.gui_id),
|
||||
messages.StatusMessage(name=self.gui_id, status=1, info={}),
|
||||
expire=10,
|
||||
)
|
||||
time.sleep(3)
|
||||
|
||||
|
||||
if __name__ == "__main__": # pragma: no cover
|
||||
import argparse
|
||||
import sys
|
||||
|
||||
from qtpy.QtWidgets import QApplication
|
||||
|
||||
app = QApplication(sys.argv)
|
||||
|
Reference in New Issue
Block a user