diff --git a/docs/widget_views.md b/docs/widget_views.md index d225b35..575bdb1 100644 --- a/docs/widget_views.md +++ b/docs/widget_views.md @@ -13,7 +13,9 @@ two independent implementations with different capabilities, described below. The desktop app's dockable property grid ({py:class}`~eco.widgets.display_qt.DisplayQt`, also used standalone via `make_assembly_qt_window`) shows a **name / current / -control** row per item, polled on a background thread: +control** row per item. Items that can push updates (motor records, PV-backed +adjustables and detectors) are monitored and show changes at once; the rest +are polled in parallel on background threads: ![Default Qt property-grid rows for a numeric Adjustable ("energy"), an enum Adjustable ("shutter"), and a read-only Detector ("intensity"). The numeric row has a step field, up/down tweak buttons, an absolute-value entry (commits on Enter/focus-out), and Stop/Reset buttons; the enum row is a dropdown with the same Stop/Reset pair; the Detector row is plain read-only text.](images/widget_default_row_qt.png) @@ -25,8 +27,29 @@ control** row per item, polled on a background thread: - **Enum-valued Adjustables** (anything exposing `enum_strs`, e.g. `AdjustablePvEnum`) get a dropdown instead of step/absolute controls, same Stop/Reset pair. +- The **tweak step** of a motor record (and the SmarAct records) starts from + the IOC's own tweak value (TWV), and editing it sets TWV. For other + adjustables it starts from the adjustable's own tweak step if one was set + up, else 1 or 0.1. +- A **status dot** at the start of the controls shows what the row is doing: + grey — no value yet (connecting); orange — sending or moving; green — + connected and monitored; blue — connected and polled; red — not connected, + failed, or blocked (e.g. the adjustable is used by a running queue task). + Hover over it for the reason. +- **Writes never block the window**: a click starts the move in the + background, so the window stays responsive and 🛑 Stop works during the + move. Opening waits at most 2 s for initial values; a row still connecting + gets its controls when its value arrives. - Right-clicking a name label opens **"Add indicator"** — see below. +In JupyterLab and Voila, a device's `.widget()` is the ipywidgets version of +the same grid (`eco.widgets.display_widget`). It follows the same rules: +- the same status dot (hover for the reason); +- monitored rows; +- the tweak step from TWV; +- clicks that never block the kernel, so 🛑 Stop works during a move; +- values that keep updating in the browser while a cell runs. + ### ipywidgets (`eco.widgets.assembly_browser`) The browser-side namespace/assembly browser used by the `lab`/`voila` diff --git a/eco/widgets/assembly_browser.py b/eco/widgets/assembly_browser.py index a1c8257..91ae00c 100644 --- a/eco/widgets/assembly_browser.py +++ b/eco/widgets/assembly_browser.py @@ -40,22 +40,21 @@ def _safe_get_value(obj: Any): def _safe_set_value(obj: Any, val): - """Try a few common setters used in eco for adjustables.""" - # try common setter names - for fn in ("set_target_value", "mv", "mvr", "set", "set_value", "put", "write"): - try: - fnobj = getattr(obj, fn) - if callable(fnobj): - return fnobj(val) - except Exception: - pass + """Set an absolute target with the first setter `obj` has. + + The first setter found is the one used, and its error is raised: this + used to swallow it and try the next name, so a set_target_value() + refused for a good reason (limits, a move blocked by a running queue + task) fell through to mv() and then to mvr() -- a *relative* move by the + absolute value.""" + for fn in ("set_target_value", "set", "set_value", "put", "write"): + fnobj = getattr(obj, fn, None) + if callable(fnobj): + return fnobj(val) # try attribute assignment if has property 'value' if hasattr(obj, "value"): - try: - setattr(obj, "value", val) - return True - except Exception: - pass + setattr(obj, "value", val) + return True raise RuntimeError("No known setter found on object") @@ -140,13 +139,18 @@ class _ItemWidget: v = txt except Exception: v = txt - try: - _safe_set_value(self.obj, v) - # auto refresh after set - time.sleep(0.01) - self.refresh() - except Exception as e: - self.value_label.value = f"Set failed: {e}" + # off the kernel thread: a move must not block other widget messages + def _set(): + try: + changer = _safe_set_value(self.obj, v) + self.value_label.value = f"setting {v!r}..." + if hasattr(changer, "wait"): + changer.wait() + self.refresh() + except Exception as e: + self.value_label.value = f"Set failed: {e}" + + threading.Thread(target=_set, daemon=True).start() def refresh(self): try: diff --git a/eco/widgets/display_qt.py b/eco/widgets/display_qt.py index be9c717..5478aab 100644 --- a/eco/widgets/display_qt.py +++ b/eco/widgets/display_qt.py @@ -24,22 +24,66 @@ that same loop instead of fighting it. If no loop is active yet, it enables one via `ip.enable_gui("qt")`. If a *different*, incompatible loop is active (e.g. `tk`), a non-blocking window isn't possible, so it falls back to a blocking `run()`. + +Responsiveness rules (each one fixed a way the window used to freeze or +leave you guessing): + +- **Nothing that talks to hardware runs on the GUI thread.** Every write + (tweak, absolute entry, enum selection, reset, stop) runs on a background + thread; the row's status dot shows what is going on -- grey: waiting for + the first value (connecting), orange: sending / moving, green: connected + and updated by a monitor, blue: connected and polled, red: not connected, + failed, or blocked (e.g. claimed by a running queue task) with the reason + as tooltip. Stop stays clickable while a move runs. Before, a click + blocked the whole window for up to 5 s (``changer.wait(timeout=5)`` in + the Qt slot) -- including the Stop button of that very move. +- **Monitorable items are monitored**: anything with + ``add_value_callback`` (motor records, SmarAct, ...) or + ``set_current_value_callback`` (PV-backed adjustables/detectors, virtual + adjustables whose parents are) pushes "value changed", and the row + re-reads at once (``get_current_value()``, a cached lookup for a + monitored channel -- so enums/conversions show exactly as when polled), + coalesced to at most 1/MIN_READ_INTERVAL per second per row. Polling + stays as a slow safety net for those, and as the normal path for the + rest. +- **Reads run in parallel** (a small thread pool), so one slow or absent + channel no longer delays every row behind it; a slow item is still polled + less often (adaptive back-off, as before). +- **Opening is bounded**: initial values are read concurrently with a + deadline; a row whose value hasn't arrived shows "connecting..." and + gets its controls once it does. +- **Tweak step**: for a motor record with its own tweak fields (see + eco.widgets.tweak_panel.has_ioc_tweak) the step field starts from the + IOC's TWV and editing it writes TWV back (as caQtDM's motor panel does); + otherwise from the adjustable's own tweak step if one was set up + (eco.manual_control.step_store.tweak_step_size), else 1 / 0.1. + +The reading/writing machinery is toolkit-independent and shared with the +ipywidgets (display_widget) and Tk (display_tk) grids: see +eco.widgets.live_values. The window also stays live while the session's main +thread is busy -- see eco.utilities.mainthread. """ +import collections import enum import threading import time -from concurrent.futures import ThreadPoolExecutor from qtpy import QtWidgets, QtCore from typing import Any +from eco.widgets import live_values +from eco.widgets.live_values import ( # the toolkit-independent part, shared with display_widget/display_tk + LiveReader, + PENDING as _PENDING, + PREFETCH_ERROR as _PREFETCH_ERROR, + default_step_for as _default_step_for, + describe as _describe, + format_value as _format_value, + initial_step as _initial_step, + prefetch_values as _prefetch_values, +) -def _format_value(value): - """Show enum-valued readbacks by their label (describer) rather than the - underlying integer.""" - if isinstance(value, enum.Enum): - return value.name - return str(value) +FLUSH_MS = 100 # GUI refresh of new values def _enum_options(item, cur): @@ -66,46 +110,11 @@ def _enum_options(item, cur): _app_ref = None # keep a strong reference to any QApplication we create ourselves -_PREFETCH_ERROR = object() # sentinel: a prefetched read raised - - -def _prefetch_values(items, max_workers=8): - """Read all initial values concurrently so building the window for a large - assembly is bounded by the slowest single readback rather than their sum. - Returns {id(item): value_or__PREFETCH_ERROR}. Never raises.""" - values = {} - items = list(items) - if not items: - return values - - def _read(it): - return it.get_current_value() - - try: - with ThreadPoolExecutor(max_workers=min(max_workers, len(items))) as ex: - futures = {ex.submit(_read, it): it for it in items} - for fut, it in futures.items(): - try: - values[id(it)] = fut.result() - except Exception: - values[id(it)] = _PREFETCH_ERROR - except Exception: - pass - return values - -# Adaptive polling: an item whose get_current_value() is slow to respond gets -# polled less often, so slow/blocking readbacks don't starve the GUI and the -# fast items. An item's poll interval becomes ~ max(base, duration * FACTOR), -# capped at MAX_INTERVAL. Fast items stay at the base interval. On Qt the poll -# also runs off the GUI thread, so a slow readback never freezes the window. -_POLL_SLOWDOWN_FACTOR = 10.0 -_POLL_MAX_INTERVAL = 30.0 - class _PollBridge(QtCore.QObject): - """Marshals value updates from the background poll thread to the GUI - thread (emitting a Qt signal across threads is thread-safe; the connected - slot then runs on the GUI thread where widgets may be touched).""" + """Marshals updates from background threads to the GUI thread (emitting + a Qt signal across threads is thread-safe; the connected slot then runs + on the GUI thread where widgets may be touched).""" result = QtCore.Signal(int, str) trigger_failed = QtCore.Signal(object) # the button widget to flash @@ -162,26 +171,131 @@ def _coerce_like(value: Any, reference: Any): return value -def _default_step_for(value: Any): - if isinstance(value, int) and not isinstance(value, bool): - return 1 - return 0.1 - - def _is_tweakable(value: Any) -> bool: """Only plain numeric values support +/- step tweaking; e.g. strings don't.""" return isinstance(value, (int, float)) and not isinstance(value, bool) def _flash_error(widget): + def _unflash(): + # the widget may be gone by now (window closed): an exception raised + # in a Qt timer callback would abort the whole process under PyQt + try: + widget.setStyleSheet("") + except RuntimeError: + pass + try: widget.setStyleSheet("background-color: #ffb3b3;") - QtCore.QTimer.singleShot(1200, lambda: widget.setStyleSheet("")) + QtCore.QTimer.singleShot(1200, _unflash) except Exception: pass -def _build_item_controls_qt(item, value_label, cur): +# -- one row: writes off the GUI thread, and its status dot ------------------------ + + +class _RowController(QtCore.QObject): + """What one row does to its item, never on the GUI thread (see + eco.widgets.live_values), and the status dot that shows it (see the + module docstring for the colours). Results come back through signals, + so the dot is only ever touched on the GUI thread.""" + + state_changed = QtCore.Signal(str, str) # state, message + read_requested = QtCore.Signal() # a write finished: re-read the value now + step_loaded = QtCore.Signal(str) # IOC tweak step arrived + _flash = QtCore.Signal(object) # a widget to flash red + + def __init__(self, item, parent=None): + super().__init__(parent) + self.item = item + self._holder = {} # "changer" of the latest write + self.dot = QtWidgets.QLabel("●") + self.dot.setFixedWidth(16) + self._read_state = ("waiting", "") + self._writing = 0 # moves in progress + self._writing_lock = threading.Lock() + self.state_changed.connect(self._show) + self._show("waiting", "") + + @property + def changer(self): + return self._holder.get("changer") + + def _show(self, state, message): + color, tooltip = live_values.dot_state(state, message, self._read_state) + try: + self.dot.setStyleSheet(f"color: {color};") + self.dot.setToolTip(tooltip) + except RuntimeError: # the row's widgets are gone (window closed) + pass + + # -- reads (called on the GUI thread) -------------------------------------- + + def read_ok(self, monitor_state): + """monitor_state: LiveReader.monitor_state() ("live"/"subscribed"/None).""" + self._read_state = (monitor_state or "polled", "") + if not self._writing: + self._show(*self._read_state) + + def read_failed(self, message): + self._read_state = ("error", message) + if not self._writing: + self._show(*self._read_state) + + # -- writes ---------------------------------------------------------------- + + def write(self, target_fn, flash=None): + """Run ``set_target_value(target_fn())`` in a background thread and + follow the move (live_values.start_write). `flash` (a widget) + flashes red on failure.""" + with self._writing_lock: + self._writing += 1 + self._show("busy", "") + + def on_state(state, message): + if state in ("idle", "error"): + with self._writing_lock: + self._writing -= 1 + self.state_changed.emit(state, message) + if state == "error" and flash is not None: + self._flash.emit(flash) + + live_values.start_write( + self.item, target_fn, on_state, on_finished=self.read_requested.emit, holder=self._holder + ) + + def stop(self): + """Stop the move this row started (or the item itself).""" + live_values.stop_move(self.item, self.changer, self.state_changed.emit) + + # -- IOC tweak step ---------------------------------------------------------- + + def attach_ioc_step(self, step_edit): + """Fill `step_edit` from the IOC's TWV (in the background) and write + edits back to it.""" + step_edit.setToolTip("tweak step, from the IOC (TWV) -- editing it sets TWV") + + def apply(text): + try: + if not step_edit.hasFocus(): + step_edit.setText(text) + except RuntimeError: # closed meanwhile + pass + + def store(): + try: + value = float(step_edit.text()) + except (ValueError, RuntimeError): + return + live_values.store_ioc_step(holder, value) + + self.step_loaded.connect(apply) + step_edit.editingFinished.connect(store) + holder = live_values.load_ioc_step(self.item, lambda value: self.step_loaded.emit(f"{value:g}")) + + +def _build_item_controls_qt(item, value_label, cur, controller=None): """Build the control area (everything right of name/value) for one item -- the exact branch logic DisplayQt._build_row applies per item in the assembly grid (Detector-read-only / Adjustable-enum / @@ -190,20 +304,26 @@ def _build_item_controls_qt(item, value_label, cur): eco.widgets.containers.adjustable_control/detector_indicator) as well as from the grid itself -- not a second implementation. - `value_label` is the QLabel the caller already built for the current - value -- writes made from here (tweak/absolute-set/enum-select/reset) - update it directly, exactly as before; reading it back (polling) is - the caller's job. + Every write goes through `controller` (a _RowController; one is made if + not given), i.e. off the GUI thread; the new value shows through the + caller's reads (polling/monitoring), which the controller asks for when + a write finishes. Its status dot is the first widget of the controls. Returns (controls: QWidget, combo: QComboBox|None) -- `controls` wraps everything in one QHBoxLayout (zero margins, so it looks identical to the same widgets living directly in the caller's own row - layout); `combo`, if not None, is what a caller polling `item` needs - to keep in sync with new readings (see _apply_value_label_update).""" + layout) and carries the controller as ``controls.controller``; `combo`, + if not None, is what a caller polling `item` needs to keep in sync with + new readings (see _apply_value_label_update).""" controls = QtWidgets.QWidget() row = QtWidgets.QHBoxLayout(controls) row.setContentsMargins(0, 0, 0, 0) combo = None + if controller is None: + controller = _RowController(item, parent=controls) + controller._flash.connect(_flash_error) + controls.controller = controller + row.addWidget(controller.dot) # Detector (non-Adjustable): read-only if isinstance(item, Detector) and not isinstance(item, Adjustable): @@ -213,7 +333,6 @@ def _build_item_controls_qt(item, value_label, cur): elif isinstance(item, Adjustable): original_value = cur enum_opts = _enum_options(item, cur) - changer_ref = {"changer": None} if enum_opts is not None: # ENUM adjustable: dropdown selector, current readback preselected @@ -224,53 +343,19 @@ def _build_item_controls_qt(item, value_label, cur): combo.setCurrentText(cur_label) combo.setFixedWidth(140) - def _enum_set(label): - try: - r = item.set_target_value(label) - changer_ref["changer"] = r - try: - if hasattr(r, "wait"): - r.wait(timeout=5) - except Exception: - pass - try: - new_cur = item.get_current_value() - except Exception: - new_cur = None - if new_cur is not None: - lbl = _format_value(new_cur) - value_label.setText(lbl) - idx = combo.findText(lbl) - if idx >= 0: - combo.blockSignals(True) - combo.setCurrentIndex(idx) - combo.blockSignals(False) - except Exception: - _flash_error(combo) - def _on_activated(index=0): - _enum_set(combo.currentText()) - - def _on_stop(checked=False): - changer = changer_ref.get("changer") - if changer is not None and hasattr(changer, "stop"): - try: - changer.stop() - except Exception: - pass + label = combo.currentText() + controller.write(lambda: label, flash=combo) def _on_reset(checked=False): - _enum_set( - original_value.name - if isinstance(original_value, enum.Enum) - else original_value - ) + target = original_value.name if isinstance(original_value, enum.Enum) else original_value + controller.write(lambda: target, flash=combo) combo.activated.connect(_on_activated) stop_btn = QtWidgets.QPushButton("🛑") stop_btn.setFixedWidth(30) stop_btn.setToolTip("Stop the current move") - stop_btn.clicked.connect(_on_stop) + stop_btn.clicked.connect(lambda checked=False: controller.stop()) reset_btn = QtWidgets.QPushButton("↺") reset_btn.setFixedWidth(30) reset_btn.setToolTip("Reset to the value from when this widget was opened") @@ -289,30 +374,23 @@ def _build_item_controls_qt(item, value_label, cur): tweakable = _is_tweakable(cur) if tweakable: - step_edit = QtWidgets.QLineEdit(str(_default_step_for(cur))) + step_edit = QtWidgets.QLineEdit(str(_initial_step(item, cur))) step_edit.setFixedWidth(60) row.addWidget(step_edit) + if live_values.has_ioc_step(item): + controller.attach_ioc_step(step_edit) if is_plain_scalar: input_edit = QtWidgets.QLineEdit(str(cur)) input_edit.setFixedWidth(100) - def _apply_absolute(it=item, vl=value_label, ie=input_edit, ref=cur, - cref=changer_ref): + def _apply_absolute(ie=input_edit, ref=cur): try: - newval = _coerce_like(ie.text(), ref) - r = it.set_target_value(newval) - cref["changer"] = r - try: - if hasattr(r, "wait"): - r.wait(timeout=5) - except Exception: - pass - new_current = it.get_current_value() - vl.setText(_format_value(new_current)) - ie.setText(str(new_current)) + target = _coerce_like(ie.text(), ref) except Exception: _flash_error(ie) + return + controller.write(lambda: target, flash=ie) input_edit.editingFinished.connect(_apply_absolute) else: @@ -320,26 +398,17 @@ def _build_item_controls_qt(item, value_label, cur): input_edit.setFixedWidth(100) if tweakable: - def _make_tweak(sign, it=item, vl=value_label, se=step_edit, - ie=input_edit, plain=is_plain_scalar, cref=changer_ref): + + def _make_tweak(sign, se=step_edit, ie=input_edit): def _on_click(): try: step = _coerce_like(se.text(), _default_step_for(cur)) - base = it.get_current_value() - newval = base + sign * step - r = it.set_target_value(newval) - cref["changer"] = r - try: - if hasattr(r, "wait"): - r.wait(timeout=5) - except Exception: - pass - new_current = it.get_current_value() - vl.setText(_format_value(new_current)) - if plain: - ie.setText(str(new_current)) except Exception: - _flash_error(ie if isinstance(ie, QtWidgets.QLineEdit) else vl) + _flash_error(se) + return + flash = ie if isinstance(ie, QtWidgets.QLineEdit) else se + # relative to the real current value, read in the background + controller.write(lambda: item.get_current_value() + sign * step, flash=flash) return _on_click @@ -354,36 +423,14 @@ def _build_item_controls_qt(item, value_label, cur): row.addWidget(input_edit) - def _on_stop(checked=False, it=item, cref=changer_ref): - changer = cref.get("changer") - if changer is not None and hasattr(changer, "stop"): - try: - changer.stop() - except Exception: - pass - - def _on_reset(checked=False, it=item, vl=value_label, ie=input_edit, - plain=is_plain_scalar, ref=original_value, cref=changer_ref): - try: - r = it.set_target_value(ref) - cref["changer"] = r - try: - if hasattr(r, "wait"): - r.wait(timeout=5) - except Exception: - pass - new_current = it.get_current_value() - vl.setText(_format_value(new_current)) - if plain: - ie.setText(str(new_current)) - except Exception: - if isinstance(ie, QtWidgets.QLineEdit): - _flash_error(ie) + def _on_reset(checked=False, ie=input_edit, ref=original_value): + flash = ie if isinstance(ie, QtWidgets.QLineEdit) else None + controller.write(lambda: ref, flash=flash) stop_btn = QtWidgets.QPushButton("🛑") stop_btn.setFixedWidth(30) stop_btn.setToolTip("Stop the current move") - stop_btn.clicked.connect(_on_stop) + stop_btn.clicked.connect(lambda checked=False: controller.stop()) reset_btn = QtWidgets.QPushButton("↺") reset_btn.setFixedWidth(30) reset_btn.setToolTip("Reset to the value from when this widget was opened") @@ -399,20 +446,13 @@ def _build_item_controls_qt(item, value_label, cur): ): input_edit = QtWidgets.QLineEdit(str(cur)) - def _apply(it=item, vl=value_label, ie=input_edit, ref=cur): + def _apply(ie=input_edit, ref=cur): try: - newval = _coerce_like(ie.text(), ref) - r = it.set_target_value(newval) - try: - if hasattr(r, "wait"): - r.wait(timeout=5) - except Exception: - pass - new_current = it.get_current_value() - vl.setText(str(new_current)) - ie.setText(str(new_current)) + target = _coerce_like(ie.text(), ref) except Exception: _flash_error(ie) + return + controller.write(lambda: target, flash=ie) input_edit.editingFinished.connect(_apply) row.addWidget(input_edit) @@ -445,12 +485,10 @@ def _apply_value_label_update(value_label, combo, text): class _StandaloneItemBridge(QtCore.QObject): - """Marshals one item's polled value from the background thread to the - GUI thread -- same pattern as _PollBridge, just for a single - standalone control (see build_adjustable_control_qt) rather than a - whole assembly grid's shared poll thread.""" + """Marshals one item's read from the background thread to the GUI + thread -- the single-row counterpart of DisplayQt's own flushing.""" - result = QtCore.Signal(str) + result = QtCore.Signal(bool, object) # ok, value | message def build_adjustable_control_qt(item, poll_interval=1.0, title=None): @@ -459,9 +497,11 @@ def build_adjustable_control_qt(item, poll_interval=1.0, title=None): the normal assembly grid, e.g. via eco.widgets.containers.adjustable_control()/detector_indicator() (the same function serves both -- _build_item_controls_qt already renders - read-only for a plain, non-Adjustable Detector). Returns an object - with `.window` (QWidget) and `.stop()`, matching eco's usual Qt - widget-wrapper convention (see EcoDesktopApp._dock_widget_object).""" + read-only for a plain, non-Adjustable Detector). Monitored where the + item supports it, polled otherwise, writes off the GUI thread -- same + rules as the grid. Returns an object with `.window` (QWidget) and + `.stop()`, matching eco's usual Qt widget-wrapper convention (see + EcoDesktopApp._dock_widget_object).""" from eco.widgets.qt_lifecycle import close_calls_stop name = title or _label_of(item) @@ -482,31 +522,33 @@ def build_adjustable_control_qt(item, poll_interval=1.0, title=None): row.addWidget(value_label) controls, combo = _build_item_controls_qt(item, value_label, cur) + controller = controls.controller row.addWidget(controls) bridge = _StandaloneItemBridge() - bridge.result.connect(lambda text: _apply_value_label_update(value_label, combo, text)) + reader = LiveReader(lambda index, ok, payload: bridge.result.emit(ok, payload), poll_interval=poll_interval, workers=1) + reader.add(item) - stop_event = threading.Event() + def _on_result(ok, payload): + try: + if ok: + _apply_value_label_update(value_label, combo, _format_value(payload)) + controller.read_ok(reader.monitor_state(0)) + else: + controller.read_failed(payload) + except RuntimeError: # a read that arrived after the control was closed + pass - def _poll_loop(): - while not stop_event.is_set(): - try: - text = _format_value(item.get_current_value()) - except Exception: - text = "" - bridge.result.emit(text) - stop_event.wait(poll_interval) - - poll_thread = threading.Thread(target=_poll_loop, daemon=True) - poll_thread.start() + bridge.result.connect(_on_result) + controller.read_requested.connect(lambda: reader.request_read(0)) + reader.start() class _StandaloneControl: def __init__(self): self.window = window def stop(self): - stop_event.set() + reader.stop() if self.window is not None: self.window.close() self.window = None @@ -528,11 +570,11 @@ class DisplayQt: self.poll_interval = poll_interval self.show_hidden = show_hidden self.window = None - self._entries = [] # list of dicts: item, value_label, active, interval, next_due + self._entries = [] # one dict per polled row, see _build_row self._child_windows = {} # id(child_assembly) -> DisplayQt shown in its own window self._bridge = None - self._poll_thread = None - self._stop_event = threading.Event() + self._reader = None # live_values.LiveReader, see _build_window + self._results = collections.deque() # (idx, ok, value | message) from reader threads self._prefetched = {} self._memory_browser = None # lazily-built "memories" sub-window, see _open_memory_browser @@ -629,22 +671,39 @@ class DisplayQt: name_label.setFixedWidth(200) row.addWidget(name_label) - value_label = QtWidgets.QLabel(_format_value(cur)) + pending = cur is _PENDING + value_label = QtWidgets.QLabel("…" if pending else _format_value(cur)) value_label.setFixedWidth(140) row.addWidget(value_label) - controls, combo = _build_item_controls_qt(item, value_label, cur) + controller = _RowController(item, parent=self.window) + if pending: + # no value yet, so no way to tell which controls fit (enum? + # numeric?): built when the first value arrives, see _flush + controls, combo = QtWidgets.QLabel("connecting…"), None + placeholder = QtWidgets.QWidget() + placeholder_row = QtWidgets.QHBoxLayout(placeholder) + placeholder_row.setContentsMargins(0, 0, 0, 0) + placeholder_row.addWidget(controller.dot) + placeholder_row.addWidget(controls) + placeholder_row.addStretch(1) + controls = placeholder + else: + controls, combo = _build_item_controls_qt(item, value_label, cur, controller=controller) row.addWidget(controls) layout.addLayout(row) + index = self._reader.add(item, active=active) # same index as in self._entries + controller.read_requested.connect(lambda i=index: self._reader.request_read(i)) self._entries.append( { "item": item, "value_label": value_label, "combo": combo, - "active": active, - "interval": self.poll_interval, - "next_due": 0.0, # monotonic time; 0 => poll on first pass + "controller": controller, + "controls": controls, + "row": row, + "needs_controls": pending, } ) @@ -654,6 +713,9 @@ class DisplayQt: # should normally already exist (created by IPython's qt inputhook, # or by run() below), but guard against it missing regardless _app_ref = QtWidgets.QApplication([]) + from eco.utilities import mainthread + + mainthread.install_qt() # stays live while the session is busy self.window = QtWidgets.QWidget() self.window.setWindowTitle( @@ -692,10 +754,17 @@ class DisplayQt: display_items = self._get_display_items() hidden_items = self._get_hidden_items() # read every initial value concurrently up front, so opening a large - # assembly is bounded by the slowest single readback, not their sum - self._prefetched = _prefetch_values(list(display_items) + list(hidden_items)) + # assembly is bounded by the slowest single readback, not their sum -- + # and by PREFETCH_DEADLINE at most (a late row shows "connecting...") + self._prefetched = _prefetch_values( + list(display_items) + list(hidden_items), deadline=live_values.PREFETCH_DEADLINE + ) self._entries = [] + self._reader = LiveReader( + lambda index, ok, payload: self._results.append((index, ok, payload)), + poll_interval=self.poll_interval, + ) for item in display_items: self._build_row(item, content_layout) @@ -722,16 +791,14 @@ class DisplayQt: def _toggle_hidden( checked=False, btn=toggle_btn, box=hidden_container, - entries=hidden_entries, + entries=hidden_entries, start=hidden_start, ): showing = box.isVisible() box.setVisible(not showing) btn.setText("expand hidden" if showing else "hide hidden") - # only poll expanded items, so hidden ones don't consume resources - for ent in entries: - ent["active"] = not showing - if not showing: - ent["next_due"] = 0.0 # refresh immediately on expand + # only read/monitor expanded items, so hidden ones don't consume resources + for index in range(start, start + len(entries)): + self._reader.set_active(index, not showing) toggle_btn.clicked.connect(_toggle_hidden) @@ -760,11 +827,15 @@ class DisplayQt: close_calls_stop(self.window, self.stop) - # poll off the GUI thread so a slow readback never freezes the window; - # results are marshalled back to the GUI thread via the bridge signal - self._stop_event = threading.Event() - self._poll_thread = threading.Thread(target=self._poll_loop, daemon=True) - self._poll_thread.start() + # new values reach the widgets here, on the GUI thread, at most every + # FLUSH_MS -- a fast monitored channel can't flood the event loop + self._flush_timer = QtCore.QTimer(self.window) + self._flush_timer.timeout.connect(self._flush) + self._flush_timer.start(FLUSH_MS) + + # reads and monitor subscriptions run off the GUI thread; results + # come back through self._results (see _flush) + self._reader.start() self._cap_window_size() self.window.show() @@ -810,48 +881,44 @@ class DisplayQt: self._memory_browser = make_memory_browser_qt(self.assembly, parent=None) + # -- values: read off the GUI thread, shown on it -------------------------- + def _apply_update(self, idx, text): - # runs on the GUI thread (queued signal) - safe to touch widgets here + # runs on the GUI thread - safe to touch widgets here try: entry = self._entries[idx] _apply_value_label_update(entry["value_label"], entry.get("combo"), text) except Exception: pass - def _poll_loop(self): - # background thread: poll each active item only when due, backing off - # items whose readback is slow so they don't starve the GUI / fast items - base = self.poll_interval - entries = self._entries - while not self._stop_event.is_set(): - now = time.monotonic() - next_wakeup = now + base - for idx, ent in enumerate(entries): - if self._stop_event.is_set(): - break - if not ent.get("active", True): - continue - if now < ent["next_due"]: - next_wakeup = min(next_wakeup, ent["next_due"]) - continue - t0 = time.monotonic() - text = None - try: - text = _format_value(ent["item"].get_current_value()) - except Exception: - text = None - duration = time.monotonic() - t0 - ent["interval"] = min( - _POLL_MAX_INTERVAL, - max(base, duration * _POLL_SLOWDOWN_FACTOR), - ) - ent["next_due"] = time.monotonic() + ent["interval"] - if text is not None: - self._bridge.result.emit(idx, text) - next_wakeup = min(next_wakeup, ent["next_due"]) - sleep_for = max(0.05, next_wakeup - time.monotonic()) - if self._stop_event.wait(sleep_for): - break + def _flush(self): + """GUI thread: show what the reads brought in.""" + latest = {} + while self._results: + idx, ok, payload = self._results.popleft() + latest[idx] = (ok, payload) + for idx, (ok, payload) in latest.items(): + ent = self._entries[idx] + controller = ent["controller"] + if not ok: + if ent["needs_controls"]: + ent["value_label"].setText("—") + controller.read_failed(payload) + continue + if ent["needs_controls"]: + self._build_late_controls(ent, payload) + _apply_value_label_update(ent["value_label"], ent.get("combo"), _format_value(payload)) + controller.read_ok(self._reader.monitor_state(idx)) + + def _build_late_controls(self, ent, value): + """The first value of a row that had none at opening arrived: now + its controls (enum? numeric?) can be built.""" + controls, combo = _build_item_controls_qt( + ent["item"], ent["value_label"], value, controller=ent["controller"] + ) + ent["row"].replaceWidget(ent["controls"], controls) + ent["controls"].deleteLater() + ent["controls"], ent["combo"], ent["needs_controls"] = controls, combo, False def run(self): """Build and run the window with a blocking Qt event loop. Use this @@ -923,10 +990,11 @@ class DisplayQt: except Exception: pass self._memory_browser = None - # signal the daemon poll thread to exit (no join: it may be mid-readback, - # and we don't want to block the GUI thread waiting on it) - self._stop_event.set() - self._poll_thread = None + # stop reading (no join: a read may be in flight, and we don't want + # to block the GUI thread waiting on it); monitors are removed in + # the background + if self._reader is not None: + self._reader.stop() if self.window is not None: try: self.window.close() diff --git a/eco/widgets/display_tk.py b/eco/widgets/display_tk.py index 84dd494..3519bfe 100644 --- a/eco/widgets/display_tk.py +++ b/eco/widgets/display_tk.py @@ -20,13 +20,34 @@ this) the window is never driven from a background thread. Inside IPython, `start()` uses IPython's own Tk event-loop integration (the same mechanism `%matplotlib tk` relies on) to pump the window between prompts on the main thread; outside of IPython it falls back to a blocking mainloop. + +Same responsiveness rules as the Qt grid (eco.widgets.display_qt; the shared +machinery is eco.widgets.live_values): reads run in a background pool -- +monitored where the item supports it, polled otherwise -- and writes in +background threads, so the window never blocks on hardware (before, every +poll read all rows *on the Tk thread*, and every click waited up to 5 s for +its move). Since Tk is not thread-safe, everything those threads report goes +through a queue that the Tk thread drains every FLUSH_MS (see _post/_drain). +A status dot per row shows connecting / moving / monitored / polled / error; +hovering over it shows the reason in the status line at the bottom. """ +import collections import enum +import threading import tkinter as tk from tkinter import ttk -from concurrent.futures import ThreadPoolExecutor from typing import Any, List +from eco.widgets import live_values +from eco.widgets.live_values import ( # the toolkit-independent part, shared with display_qt/display_widget + LiveReader, + PENDING as _PENDING, + PREFETCH_ERROR as _PREFETCH_ERROR, + initial_step as _initial_step, +) + +FLUSH_MS = 100 # how often the Tk thread shows what the background threads reported + # Try to import types for isinstance checks if available. try: from eco import Adjustable, Detector @@ -45,7 +66,6 @@ except Exception: Assembly = object -_PREFETCH_ERROR = object() # sentinel: a prefetched read raised def _format_value(value): @@ -79,29 +99,9 @@ def _enum_options(item, cur): return None -def _prefetch_values(items, max_workers=8): - """Read all initial values concurrently so building the window for a large - assembly is bounded by the slowest single readback rather than their sum. - Returns {id(item): value_or__PREFETCH_ERROR}. Never raises.""" - values = {} - items = list(items) - if not items: - return values - - def _read(it): - return it.get_current_value() - - try: - with ThreadPoolExecutor(max_workers=min(max_workers, len(items))) as ex: - futures = {ex.submit(_read, it): it for it in items} - for fut, it in futures.items(): - try: - values[id(it)] = fut.result() - except Exception: - values[id(it)] = _PREFETCH_ERROR - except Exception: - pass - return values +def _prefetch_values(items, max_workers=8, deadline=None): + """See eco.widgets.live_values.prefetch_values.""" + return live_values.prefetch_values(items, max_workers=max_workers, deadline=deadline) def _label_of(item: Any, assembly=None) -> str: @@ -144,6 +144,70 @@ def _is_tweakable(value: Any) -> bool: return isinstance(value, (int, float)) and not isinstance(value, bool) +class _TkRowState: + """Tk counterpart of display_qt._RowController: writes go through + eco.widgets.live_values in background threads; their reports are + posted to the window's queue and applied on the Tk thread.""" + + def __init__(self, display, item, dot): + self.display = display + self.item = item + self.dot = dot + self.holder = {} # "changer" of the latest write + self.tooltip = "" + self.index = None # the item's index in the window's reader + self._read_state = ("waiting", "") + self._writing = 0 + dot.bind("", lambda _evt: display._status_var.set(self.tooltip)) + dot.bind("", lambda _evt: display._status_var.set("")) + self.show("waiting", "") + + def show(self, state, message): # Tk thread only + color, self.tooltip = live_values.dot_state(state, message, self._read_state) + try: + self.dot.configure(foreground=color) + except tk.TclError: # the window is gone + pass + + def read_ok(self, monitor_state): + """monitor_state: LiveReader.monitor_state() ("live"/"subscribed"/None).""" + self._read_state = (monitor_state or "polled", "") + if not self._writing: + self.show(*self._read_state) + + def read_failed(self, message): + self._read_state = ("error", message) + if not self._writing: + self.show(*self._read_state) + + def write(self, target_fn, flash=None): + self._writing += 1 + self.show("busy", "") + + def on_state(state, message): # a background thread + self.display._post(lambda: self._on_state(state, message, flash)) + + def on_finished(): + if self.index is not None and self.display._reader is not None: + self.display._reader.request_read(self.index) + + live_values.start_write(self.item, target_fn, on_state, on_finished=on_finished, holder=self.holder) + + def _on_state(self, state, message, flash): + if state in ("idle", "error"): + self._writing -= 1 + self.show(state, message) + if state == "error" and flash is not None: + self.display._flash_error(flash) + + def stop(self): + live_values.stop_move( + self.item, + self.holder.get("changer"), + lambda state, message: self.display._post(lambda: self.show(state, message)), + ) + + class DisplayTk: def __init__( self, @@ -158,9 +222,12 @@ class DisplayTk: self.root = None self._entries = [] # list of dicts: item, value_var, input_widget/var, reader self._child_windows = {} # id(child_assembly) -> DisplayTk shown in a Toplevel - self._poll_after_id = None + self._poll_after_id = None # the pending after() of _drain self._prefetched = {} self._memory_browser = None # lazily-built "memories" sub-window, see _open_memory_browser + self._reader = None # live_values.LiveReader, see _build_layout + self._queue = collections.deque() # callables posted from background threads, see _post + self._status_var = None if auto_start: self.start() @@ -257,13 +324,17 @@ class DisplayTk: except Exception: pass - def _toggle_hidden(self, btn, box): - if box.winfo_ismapped(): + def _toggle_hidden(self, btn, box, indices=()): + showing = box.winfo_ismapped() + if showing: box.pack_forget() btn.config(text="expand hidden") else: box.pack(fill="x", padx=4, pady=(0, 4)) btn.config(text="hide hidden") + # only read/monitor shown items, so collapsed ones don't consume resources + for index in indices: + self._reader.set_active(index, not showing) def _build_layout(self): root = self.root @@ -285,8 +356,15 @@ class DisplayTk: display_items = self._get_display_items() hidden_items = self._get_hidden_items() # read every initial value concurrently up front, so opening a large - # assembly is bounded by the slowest single readback, not their sum - self._prefetched = _prefetch_values(list(display_items) + list(hidden_items)) + # assembly is bounded by the slowest single readback, not their sum -- + # and by PREFETCH_DEADLINE at most (a late row shows "connecting...") + self._prefetched = _prefetch_values( + list(display_items) + list(hidden_items), deadline=live_values.PREFETCH_DEADLINE + ) + self._reader = LiveReader( + lambda index, ok, payload: self._post(lambda: self._apply(index, ok, payload)), + poll_interval=self.poll_interval, + ) for item in display_items: self._build_item_row(scroll_area, item) @@ -299,18 +377,22 @@ class DisplayTk: toggle_row, text="hide hidden" if self.show_hidden else "expand hidden", ) - toggle_btn.config( - command=lambda b=toggle_btn, box=hidden_box: self._toggle_hidden(b, box) - ) toggle_btn.pack(side="left") + hidden_start = len(self._entries) for item in hidden_items: - self._build_item_row(hidden_box, item) + self._build_item_row(hidden_box, item, active=self.show_hidden) + hidden_indices = [e["state"].index for e in self._entries[hidden_start:]] + toggle_btn.config( + command=lambda b=toggle_btn, box=hidden_box, ix=hidden_indices: self._toggle_hidden(b, box, ix) + ) if self.show_hidden: hidden_box.pack(fill="x", padx=4, pady=(0, 4)) ttk.Separator(root).pack(fill="x", padx=4, pady=(4, 0)) + # what the status dot under the mouse means (Tk has no tooltips) + ttk.Label(root, textvariable=self._status_var, foreground="#555555").pack(fill="x", padx=4) close_row = ttk.Frame(root) close_row.pack(fill="x", padx=4, pady=4) ttk.Button(close_row, text="Close", command=self._on_close).pack(side="left") @@ -319,7 +401,7 @@ class DisplayTk: close_row, text="memories", command=self._open_memory_browser ).pack(side="left", padx=(4, 0)) - def _build_item_row(self, parent, item): + def _build_item_row(self, parent, item, active=True): name = _label_of(item, assembly=self.assembly) prefetched = getattr(self, "_prefetched", {}) if id(item) in prefetched: @@ -331,6 +413,7 @@ class DisplayTk: cur = item.get_current_value() except Exception: cur = "" + pending = cur is _PENDING row = ttk.Frame(parent) row.pack(fill="x", padx=4, pady=2) @@ -344,12 +427,27 @@ class DisplayTk: ) else: ttk.Label(row, text=name, width=28).pack(side="left") - value_var = tk.StringVar(value=_format_value(cur)) + value_var = tk.StringVar(value="…" if pending else _format_value(cur)) ttk.Label(row, textvariable=value_var, width=20).pack(side="left") + dot = ttk.Label(row, text="●", width=2) + dot.pack(side="left") control = ttk.Frame(row) control.pack(side="left", fill="x", expand=True) - entry = {"item": item, "value_var": value_var, "reader": None} + state = _TkRowState(self, item, dot) + entry = {"item": item, "value_var": value_var, "reader": None, "state": state, + "control": control, "needs_controls": pending} + if pending: + # no value yet, so no way to tell which controls fit (enum? + # numeric?): built when the first value arrives, see _apply + ttk.Label(control, text="connecting…").pack(side="left") + else: + self._build_controls(entry, cur) + state.index = self._reader.add(item, active=active) # same index as in self._entries + self._entries.append(entry) + + def _build_controls(self, entry, cur): + item, value_var, control, state = entry["item"], entry["value_var"], entry["control"], entry["state"] # Detector (non-Adjustable): read-only if isinstance(item, Detector) and not isinstance(item, Adjustable): @@ -359,8 +457,7 @@ class DisplayTk: elif isinstance(item, Adjustable): original_value = cur enum_opts = _enum_options(item, cur) - changer_ref = {"changer": None} - entry["changer_ref"] = changer_ref + stop_btn_kwargs = dict(text="\U0001f6d1", width=3, command=state.stop) if enum_opts is not None: # ENUM adjustable: dropdown selector, current readback preselected @@ -378,58 +475,29 @@ class DisplayTk: entry["combo_var"] = combo_var entry["enum_opts"] = enum_opts - def _enum_set(label, cvar=combo_var, vv=value_var, it=item, - cref=changer_ref): - try: - r = it.set_target_value(label) - cref["changer"] = r - try: - if hasattr(r, "wait"): - r.wait(timeout=5) - except Exception: - pass - try: - new_cur = it.get_current_value() - except Exception: - new_cur = None - if new_cur is not None: - lbl = _format_value(new_cur) - vv.set(lbl) - if lbl in enum_opts: - cvar.set(lbl) - except Exception: - pass - def _on_combo_selected(_evt=None, cvar=combo_var): - _enum_set(cvar.get()) + label = cvar.get() + state.write(lambda: label) combo.bind("<>", _on_combo_selected) - def _on_stop(it=item, cref=changer_ref): - changer = cref.get("changer") - if changer is not None and hasattr(changer, "stop"): - try: - changer.stop() - except Exception: - pass - def _on_reset(ref=original_value): - _enum_set(ref.name if isinstance(ref, enum.Enum) else ref) + target = ref.name if isinstance(ref, enum.Enum) else ref + state.write(lambda: target) - stop_btn = ttk.Button(control, text="\U0001f6d1", width=3, - command=_on_stop) - stop_btn.pack(side="left", padx=(4, 0)) - reset_btn = ttk.Button(control, text="↺", width=3, command=_on_reset) - reset_btn.pack(side="left") + ttk.Button(control, **stop_btn_kwargs).pack(side="left", padx=(4, 0)) + ttk.Button(control, text="↺", width=3, command=_on_reset).pack(side="left") else: is_plain_scalar = not isinstance(cur, (list, dict, bytes, bytearray)) tweakable = _is_tweakable(cur) if tweakable: - step_var = tk.StringVar(value=str(_default_step_for(cur))) + step_var = tk.StringVar(value=str(_initial_step(item, cur))) step_entry = ttk.Entry(control, textvariable=step_var, width=8) step_entry.pack(side="left", padx=(0, 4)) + if live_values.has_ioc_step(item): + self._attach_ioc_step(item, step_var, step_entry) if is_plain_scalar: input_var = tk.StringVar(value=str(cur)) @@ -437,29 +505,13 @@ class DisplayTk: input_widget.pack(side="left", padx=(0, 4)) entry["reader"] = lambda iv=input_var: iv.get() - def _apply_absolute( - _evt=None, - it=item, - vv=value_var, - iv=input_var, - iw=input_widget, - ref=cur, - cref=changer_ref, - ): + def _apply_absolute(_evt=None, iv=input_var, iw=input_widget, ref=cur): try: - newval = _coerce_like(iv.get(), ref) - r = it.set_target_value(newval) - cref["changer"] = r - try: - if hasattr(r, "wait"): - r.wait(timeout=5) - except Exception: - pass - new_current = it.get_current_value() - vv.set(_format_value(new_current)) - iv.set(str(new_current)) + target = _coerce_like(iv.get(), ref) except Exception: self._flash_error(iw) + return + state.write(lambda: target, flash=iw) input_widget.bind("", _apply_absolute) input_widget.bind("", _apply_absolute) @@ -468,73 +520,26 @@ class DisplayTk: input_widget.pack(side="left", padx=(0, 4)) if tweakable: - # base is always read fresh from the device, and the absolute - # entry is resynced to the real current value after every move - def make_tweak_handler(sign, it=item, vv=value_var, sv=step_var, - iw=input_widget, cref=changer_ref, - input_var=(input_var if is_plain_scalar else None)): + # relative to the real current value, read in the background + def make_tweak_handler(sign, sv=step_var, iw=input_widget, it=item): def _on_click(): try: step = _coerce_like(sv.get(), _default_step_for(cur)) - base = it.get_current_value() - newval = base + sign * step - r = it.set_target_value(newval) - cref["changer"] = r - try: - if hasattr(r, "wait"): - r.wait(timeout=5) - except Exception: - pass - new_current = it.get_current_value() - vv.set(_format_value(new_current)) - if input_var is not None: - input_var.set(str(new_current)) except Exception: - self._flash_error(iw) + self._flash_error(step_entry) + return + state.write(lambda: it.get_current_value() + sign * step, flash=iw) return _on_click - up_btn = ttk.Button(control, text="▲", width=3, - command=make_tweak_handler(1)) - up_btn.pack(side="left") - down_btn = ttk.Button(control, text="▼", width=3, - command=make_tweak_handler(-1)) - down_btn.pack(side="left") + ttk.Button(control, text="▲", width=3, command=make_tweak_handler(1)).pack(side="left") + ttk.Button(control, text="▼", width=3, command=make_tweak_handler(-1)).pack(side="left") - def _on_stop(it=item, cref=changer_ref): - changer = cref.get("changer") - if changer is not None and hasattr(changer, "stop"): - try: - changer.stop() - except Exception: - pass + def _on_reset(ref=original_value, iw=input_widget if is_plain_scalar else None): + state.write(lambda: ref, flash=iw) - def _on_reset( - it=item, vv=value_var, - iw=input_widget if is_plain_scalar else None, - input_var=(input_var if is_plain_scalar else None), - ref=original_value, cref=changer_ref, - ): - try: - r = it.set_target_value(ref) - cref["changer"] = r - try: - if hasattr(r, "wait"): - r.wait(timeout=5) - except Exception: - pass - new_current = it.get_current_value() - vv.set(_format_value(new_current)) - if input_var is not None: - input_var.set(str(new_current)) - except Exception: - if iw is not None: - self._flash_error(iw) - - stop_btn = ttk.Button(control, text="🛑", width=3, command=_on_stop) - stop_btn.pack(side="left", padx=(4, 0)) - reset_btn = ttk.Button(control, text="↺", width=3, command=_on_reset) - reset_btn.pack(side="left") + ttk.Button(control, **stop_btn_kwargs).pack(side="left", padx=(4, 0)) + ttk.Button(control, text="↺", width=3, command=_on_reset).pack(side="left") # Fallback: has set_target_value but not recognized as Adjustable elif hasattr(item, "set_target_value") and callable( @@ -545,27 +550,13 @@ class DisplayTk: input_widget.pack(side="left") entry["reader"] = lambda iv=input_var: iv.get() - def _apply( - _evt=None, - it=item, - vv=value_var, - iv=input_var, - iw=input_widget, - ref=cur, - ): + def _apply(_evt=None, iv=input_var, iw=input_widget, ref=cur): try: - newval = _coerce_like(iv.get(), ref) - r = it.set_target_value(newval) - try: - if hasattr(r, "wait"): - r.wait(timeout=5) - except Exception: - pass - new_current = it.get_current_value() - vv.set(str(new_current)) - iv.set(str(new_current)) + target = _coerce_like(iv.get(), ref) except Exception: self._flash_error(iw) + return + state.write(lambda: target, flash=iw) input_widget.bind("", _apply) input_widget.bind("", _apply) @@ -573,30 +564,74 @@ class DisplayTk: else: ttk.Label(control, text="—").pack(side="left") - self._entries.append(entry) + def _attach_ioc_step(self, item, step_var, step_entry): + """Fill the step field from the IOC's TWV and write edits back.""" - def _poll(self): + def on_step(value): # a background thread + def show(): + try: + if self.root is not None and self.root.focus_get() is not step_entry: + step_var.set(f"{value:g}") + except (tk.TclError, KeyError): + pass + + self._post(show) + + holder = live_values.load_ioc_step(item, on_step) + + def store(_evt=None): + try: + live_values.store_ioc_step(holder, float(step_var.get())) + except ValueError: + pass + + step_entry.bind("", store) + step_entry.bind("", store) + + # -- what the background threads report, on the Tk thread -------------------- + + def _post(self, fn): + """Run `fn` on the Tk thread soon (from any thread).""" + self._queue.append(fn) + + def _drain(self): if self.root is None: return - for ent in self._entries: + while self._queue: + fn = self._queue.popleft() try: - val = ent["item"].get_current_value() - text = _format_value(val) - ent["value_var"].set(text) - combo_var = ent.get("combo_var") - # keep the dropdown selection tracking the readback (e.g. a - # valve settling from a stale/mismatched selection to its - # true OPEN/CLOSED state); setting the StringVar directly - # does not fire <>, so this can't loop back - # into _enum_set and issue a spurious write. - if combo_var is not None and text in ent.get("enum_opts", ()): - combo_var.set(text) + fn() + except tk.TclError: # a widget went away meanwhile + pass except Exception: pass if self.root is not None: - self._poll_after_id = self.root.after( - int(self.poll_interval * 1000), self._poll - ) + self._poll_after_id = self.root.after(FLUSH_MS, self._drain) + + def _apply(self, index, ok, payload): + """Show one read (Tk thread).""" + ent = self._entries[index] + state = ent["state"] + if not ok: + if ent["needs_controls"]: + ent["value_var"].set("—") + state.read_failed(payload) + return + if ent["needs_controls"]: + for child in ent["control"].winfo_children(): + child.destroy() + ent["needs_controls"] = False + self._build_controls(ent, payload) + text = _format_value(payload) + ent["value_var"].set(text) + combo_var = ent.get("combo_var") + # keep the dropdown selection tracking the readback (e.g. a valve + # settling from a stale/mismatched selection to its true OPEN/CLOSED + # state); setting the StringVar directly does not fire + # <>, so this can't loop back into a write. + if combo_var is not None and text in ent.get("enum_opts", ()): + combo_var.set(text) + state.read_ok(self._reader.monitor_state(index)) def _on_close(self): self.stop() @@ -635,10 +670,10 @@ class DisplayTk: self.root.protocol("WM_DELETE_WINDOW", self._on_close) self._entries = [] self._poll_after_id = None + self._status_var = tk.StringVar(value="") self._build_layout() - self._poll_after_id = self.root.after( - int(self.poll_interval * 1000), self._poll - ) + self._reader.start() + self._poll_after_id = self.root.after(FLUSH_MS, self._drain) self._cap_window_height() def run(self): @@ -694,6 +729,8 @@ class DisplayTk: except Exception: pass self._memory_browser = None + if self._reader is not None: + self._reader.stop() if self.root is not None: if getattr(self, "_poll_after_id", None) is not None: try: diff --git a/eco/widgets/display_widget.py b/eco/widgets/display_widget.py index fe2d731..13547db 100644 --- a/eco/widgets/display_widget.py +++ b/eco/widgets/display_widget.py @@ -16,17 +16,37 @@ terminal `rich` table look (`eco.utilities.tables.section_row_styles`, also used by `Assembly.get_display_str()`/`Beamline.diagram()`) so an assembly looks the same whether you're in a terminal or a notebook -- see `_section_css_classes`/`_state_css_class` below for how each is derived here. + +Same responsiveness rules as the Qt grid (see eco.widgets.display_qt's module +docstring and eco.widgets.live_values, which both share): a click never +blocks the kernel -- before, it ran ``set_target_value`` + ``wait(timeout=5)`` +in the widget callback, so the kernel handled no other widget message +(including that row's Stop) until the move ended; writes now run in a +background thread and a status dot per row shows connecting / moving / +monitored / polled / error (hover for the reason). Monitorable items are +monitored, the rest polled in parallel; opening waits at most +PREFETCH_DEADLINE for initial values; the tweak step of a motor record starts +from the IOC's TWV (edits write it back). Widget values may be set from any +thread (the change goes out as a comm message), which is what lets updates +reach the browser -- also in Voila -- while a cell is running. """ import enum +import html import threading import time -from concurrent.futures import ThreadPoolExecutor from typing import Any, List import ipywidgets as widgets from IPython.display import display from eco.utilities.tables import section_row_styles +from eco.widgets import live_values +from eco.widgets.live_values import ( # the toolkit-independent part, shared with display_qt/display_tk + LiveReader, + PENDING as _PENDING, + PREFETCH_ERROR as _PREFETCH_ERROR, + initial_step as _initial_step, +) def _format_value(value: Any) -> str: @@ -145,14 +165,6 @@ def _state_css_class(item, value): return None -# Adaptive polling: an item whose get_current_value() is slow to respond gets -# polled less often, so slow/blocking readbacks don't starve the GUI and the -# fast items. An item's poll interval becomes ~ max(base, duration * FACTOR), -# capped at MAX_INTERVAL. Fast items stay at the base interval. -_POLL_SLOWDOWN_FACTOR = 10.0 -_POLL_MAX_INTERVAL = 30.0 - -_PREFETCH_ERROR = object() # sentinel: a prefetched read raised # Responsive layout: columns use flexbox with a preferred basis + min-width and @@ -188,29 +200,9 @@ def _action_group(stop_btn, reset_btn): return widgets.HBox([stop_btn, reset_btn], layout=widgets.Layout(margin="0 0 0 auto")) -def _prefetch_values(items, max_workers=8): - """Read all initial values concurrently so building the widget for a large - assembly is bounded by the slowest single readback rather than their sum. - Returns {id(item): value_or__PREFETCH_ERROR}. Never raises.""" - values = {} - items = list(items) - if not items: - return values - - def _read(it): - return it.get_current_value() - - try: - with ThreadPoolExecutor(max_workers=min(max_workers, len(items))) as ex: - futures = {ex.submit(_read, it): it for it in items} - for fut, it in futures.items(): - try: - values[id(it)] = fut.result() - except Exception: - values[id(it)] = _PREFETCH_ERROR - except Exception: - pass - return values +def _prefetch_values(items, max_workers=8, deadline=None): + """See eco.widgets.live_values.prefetch_values.""" + return live_values.prefetch_values(items, max_workers=max_workers, deadline=deadline) # Try to import types for isinstance checks if available. try: @@ -253,16 +245,16 @@ def _make_input_widget_for_value(value: Any): return w, reader -def _make_step_widget_for_value(value: Any): - """Create step-size input suitable for numeric types.""" - if isinstance(value, int) and not isinstance(value, bool): - step_w = widgets.IntText(value=1, layout=widgets.Layout(width="80px")) +def _make_step_widget_for_value(value: Any, step=None): + """Create step-size input suitable for numeric types, starting at `step` + (default: 1 for ints, 0.1 for floats).""" + if isinstance(value, int) and not isinstance(value, bool) and (step is None or float(step).is_integer()): + step_w = widgets.IntText(value=int(step or 1), layout=widgets.Layout(width="80px")) reader = lambda: int(step_w.value) else: - step_w = widgets.FloatText( - value=0.1 if isinstance(value, float) else 1.0, - layout=widgets.Layout(width="80px"), - ) + if step is None: + step = 0.1 if isinstance(value, float) else 1.0 + step_w = widgets.FloatText(value=float(step), layout=widgets.Layout(width="80px")) reader = lambda: float(step_w.value) return step_w, reader @@ -285,7 +277,109 @@ def _flash_button_error(btn): threading.Thread(target=_reset, daemon=True).start() -def _build_item_controls_widget(item, value_w, cur): +def _flash_widget_error(widget): + """A failed write: a button shows "Err", anything else a red border, + for 1.2 s.""" + if widget is None: + return + if isinstance(widget, widgets.Button): + _flash_button_error(widget) + return + try: + old = widget.layout.border + widget.layout.border = "1px solid red" + + def _reset(o=old): + time.sleep(1.2) + widget.layout.border = o + + threading.Thread(target=_reset, daemon=True).start() + except Exception: + pass + + +class _RowState: + """ipywidgets counterpart of display_qt._RowController: writes go + through eco.widgets.live_values in background threads, and an HTML + status dot shows what the row is doing (hover for the reason). Widget + values may be set from any thread, so results are applied directly. + ``on_finished`` (set by the grid) runs after every write -- it requests + a re-read.""" + + def __init__(self, item): + self.item = item + self.holder = {} # "changer" of the latest write + self.dot = widgets.HTML(layout=widgets.Layout(width="18px", min_width="18px")) + self.on_finished = None + self._read_state = ("waiting", "") + self._writing = 0 + self._writing_lock = threading.Lock() + self.show("waiting", "") + + def show(self, state, message): + color, tooltip = live_values.dot_state(state, message, self._read_state) + self.dot.value = ( + f'●' + ) + + def read_ok(self, monitor_state): + """monitor_state: LiveReader.monitor_state() ("live"/"subscribed"/None).""" + self._read_state = (monitor_state or "polled", "") + if not self._writing: + self.show(*self._read_state) + + def read_failed(self, message): + self._read_state = ("error", message) + if not self._writing: + self.show(*self._read_state) + + def write(self, target_fn, flash=None): + with self._writing_lock: + self._writing += 1 + self.show("busy", "") + + def on_state(state, message): + if state in ("idle", "error"): + with self._writing_lock: + self._writing -= 1 + self.show(state, message) + if state == "error": + _flash_widget_error(flash) + + def on_finished(): + if self.on_finished is not None: + self.on_finished() + + live_values.start_write(self.item, target_fn, on_state, on_finished=on_finished, holder=self.holder) + + def stop(self): + live_values.stop_move(self.item, self.holder.get("changer"), self.show) + + def attach_ioc_step(self, step_w): + """Fill `step_w` from the IOC's TWV and write edits back to it.""" + suppress = [False] + step_w.description_tooltip = "tweak step, from the IOC (TWV) -- editing it sets TWV" + + def on_step(value): + suppress[0] = True + try: + step_w.value = type(step_w.value)(value) + except Exception: + pass + finally: + suppress[0] = False + + holder = live_values.load_ioc_step(self.item, on_step) + + def on_change(change): + if not suppress[0]: + live_values.store_ioc_step(holder, change["new"]) + + step_w.observe(on_change, names="value") + + +def _build_item_controls_widget(item, value_w, cur, row_state=None): """Build the control area for one item -- the exact branch logic make_assembly_widget's _build_row applies per item in the assembly grid (Detector-read-only / Adjustable-enum / Adjustable-tweakable-or- @@ -295,27 +389,29 @@ def _build_item_controls_widget(item, value_w, cur): as from the grid itself -- not a second implementation. Qt equivalent: eco.widgets.display_qt._build_item_controls_qt. - `value_w` is the Label the caller already built for the current value - -- writes made from here (tweak/absolute-set/enum-select/reset) update - it directly, exactly as before; reading it back (polling) is the - caller's job, via _apply_item_update below. + Every write goes through `row_state` (a _RowState; one is made if not + given), i.e. off the kernel's message thread; the new value shows + through the caller's reads (_apply_result), which the row state asks + for when a write finishes. Its status dot is the first widget of the + control box. Returns (control_box: widgets.HBox, poll_state: dict) -- `poll_state` - bundles whatever _apply_item_update needs beyond item/value_widget + bundles whatever _apply_result needs beyond item/value_widget (input_widget/enum_opts/dd_suppress, for keeping an enum dropdown in - sync with polled readings); merge it into an item_entries-shaped dict - alongside "item"/"value_widget" for a poll loop (the grid's shared - one, or a standalone one -- see build_adjustable_control_widget) to - drive.""" + sync with polled readings, and the row_state); merge it into an + item_entries-shaped dict alongside "item"/"value_widget".""" + if row_state is None: + row_state = _RowState(item) control_box = widgets.HBox(layout=_control_layout()) input_widget = None reader = None enum_opts = None suppress_dd = None + children = [row_state.dot] # If it's a Detector and NOT Adjustable -> no control widget (readonly) if isinstance(item, Detector) and not isinstance(item, Adjustable): - control_box.children = (widgets.Label("read-only (Detector)"),) + children.append(widgets.Label("read-only (Detector)")) # If it's Adjustable -> show tweak widget (step, up, down) or, for # enum-enabled adjustables, a dropdown of the enum options @@ -331,7 +427,7 @@ def _build_item_controls_widget(item, value_w, cur): description="↺", layout=widgets.Layout(width="40px"), tooltip="Reset to the value from when this widget was opened", ) - last_changer = {"changer": None} + stop_btn.on_click(lambda b: row_state.stop()) if enum_opts is not None: # ENUM adjustable: dropdown selector, current readback preselected @@ -342,55 +438,22 @@ def _build_item_controls_widget(item, value_w, cur): layout=widgets.Layout(width="150px"), ) input_widget = dropdown - reader = None suppress_dd = [False] - def _enum_set(label, b=None): - try: - r = item.set_target_value(label) - last_changer["changer"] = r - try: - if hasattr(r, "wait"): - r.wait(timeout=5) - except Exception: - pass - try: - new_cur = item.get_current_value() - except Exception: - new_cur = None - if new_cur is not None: - lbl = _format_value(new_cur) - value_w.value = lbl - if lbl in enum_opts: - suppress_dd[0] = True - try: - dropdown.value = lbl - finally: - suppress_dd[0] = False - except Exception: - _flash_button_error(b) - def _on_dd_change(change): if suppress_dd[0] or change.get("name") != "value": return - _enum_set(change["new"]) + label = change["new"] + row_state.write(lambda: label, flash=dropdown) def _on_enum_reset(b=None): orig = original_value - _enum_set(orig.name if isinstance(orig, enum.Enum) else orig, b) - - def _on_enum_stop(b=None): - changer = last_changer.get("changer") - if changer is not None and hasattr(changer, "stop"): - try: - changer.stop() - except Exception: - _flash_button_error(b) + target = orig.name if isinstance(orig, enum.Enum) else orig + row_state.write(lambda: target, flash=b) dropdown.observe(_on_dd_change, names="value") - stop_btn.on_click(_on_enum_stop) reset_btn.on_click(_on_enum_reset) - control_box.children = (dropdown, _action_group(stop_btn, reset_btn)) + children += [dropdown, _action_group(stop_btn, reset_btn)] else: tweakable = _is_tweakable(cur) @@ -405,81 +468,31 @@ def _build_item_controls_widget(item, value_w, cur): input_widget = widgets.Label("n/a", layout=widgets.Layout(width="80px")) reader = None - def make_handlers(it, val_widget, inp_widget, inp_reader, changer_ref): - # guards recursive triggering of the input's on-change handler - # when we update inp_widget.value ourselves after a move - suppress_input_event = [False] - - def _sync_input_widget(value): - if inp_reader is None: - return - suppress_input_event[0] = True - try: - inp_widget.value = value - except Exception: - pass - finally: - suppress_input_event[0] = False - - def _do_set(newval, btn=None): - try: - r = it.set_target_value(newval) - changer_ref["changer"] = r - try: - if hasattr(r, "wait"): - r.wait(timeout=5) - except Exception: - pass - try: - new_current = it.get_current_value() - except Exception: - new_current = newval - try: - val_widget.value = str(new_current) - except Exception: - pass - # always reflect the real current value, so the next - # tweak/move starts from where the device actually is - _sync_input_widget(new_current) - except Exception: - _flash_button_error(btn) - - def _on_input_change(change): - if suppress_input_event[0] or inp_reader is None: - return - if change.get("name") != "value": - return - _do_set(inp_reader(), None) - - def _on_stop(b=None): - changer = changer_ref.get("changer") - if changer is not None and hasattr(changer, "stop"): - try: - changer.stop() - except Exception: - _flash_button_error(b) - - def _on_reset(b=None): - _do_set(original_value, b) - - return _do_set, _on_input_change, _on_stop, _on_reset - - do_set, on_input_change, on_stop, on_reset = make_handlers( - item, value_w, input_widget, reader, last_changer - ) # set the value as soon as a new one is entered (on Enter/blur, # not per keystroke) instead of requiring a separate "Set" button if reader is not None: if hasattr(input_widget, "continuous_update"): input_widget.continuous_update = False - input_widget.observe(on_input_change, names="value") - stop_btn.on_click(on_stop) - reset_btn.on_click(on_reset) - control_children = [] + def _on_input_change(change, rd=reader, inp=input_widget): + if change.get("name") != "value": + return + try: + target = rd() + except Exception: + _flash_widget_error(inp) + return + row_state.write(lambda: target, flash=inp) + + input_widget.observe(_on_input_change, names="value") + + reset_btn.on_click(lambda b, ref=original_value: row_state.write(lambda: ref, flash=b)) + if tweakable: # only plain numbers support +/- step tweaking (e.g. not strings) - step_w, step_reader = _make_step_widget_for_value(cur) + step_w, step_reader = _make_step_widget_for_value(cur, _initial_step(item, cur)) + if live_values.has_ioc_step(item): + row_state.attach_ioc_step(step_w) up_btn = widgets.Button( description="▲", layout=widgets.Layout(width="40px") ) @@ -487,26 +500,23 @@ def _build_item_controls_widget(item, value_w, cur): description="▼", layout=widgets.Layout(width="40px") ) - def make_tweak(sign, it=item, sr=step_reader, ds=do_set): + def make_tweak(sign, it=item, sr=step_reader): def _on_click(b=None): try: step = sr() - base = it.get_current_value() - newval = base + sign * step except Exception: _flash_button_error(b) return - ds(newval, b) + # relative to the real current value, read in the background + row_state.write(lambda: it.get_current_value() + sign * step, flash=b) return _on_click up_btn.on_click(make_tweak(1)) down_btn.on_click(make_tweak(-1)) - control_children.extend([step_w, up_btn, down_btn]) + children += [step_w, up_btn, down_btn] - control_children.append(input_widget) - control_children.append(_action_group(stop_btn, reset_btn)) - control_box.children = tuple(control_children) + children += [input_widget, _action_group(stop_btn, reset_btn)] # Fallback: if item has set_target_value (callable) but wasn't captured above, allow simple set elif hasattr(item, "set_target_value") and callable( @@ -515,116 +525,107 @@ def _build_item_controls_widget(item, value_w, cur): # create input widget based on current value input_widget, reader = _make_input_widget_for_value(cur) input_widget.layout.margin = "0 6px 0 0" - - def make_on_set(it, rw, vw, inp): - def _on_set(change): - if change.get("name") != "value": - return - try: - val = rw() - r = it.set_target_value(val) - try: - if hasattr(r, "wait"): - r.wait(timeout=5) - except Exception: - pass - try: - vw.value = _format_value(it.get_current_value()) - except Exception: - pass - except Exception: - old_border = inp.layout.border - inp.layout.border = "1px solid red" - - def _reset(o=old_border): - time.sleep(1.2) - inp.layout.border = o - - threading.Thread(target=_reset, daemon=True).start() - - return _on_set - - # set the value as soon as a new one is entered (on Enter/blur, - # not per keystroke) instead of requiring a separate "Set" button if hasattr(input_widget, "continuous_update"): input_widget.continuous_update = False - input_widget.observe( - make_on_set(item, reader, value_w, input_widget), names="value" - ) - control_box.children = (input_widget,) + + def _on_set(change, rd=reader, inp=input_widget): + if change.get("name") != "value": + return + try: + target = rd() + except Exception: + _flash_widget_error(inp) + return + row_state.write(lambda: target, flash=inp) + + input_widget.observe(_on_set, names="value") + children.append(input_widget) else: - control_box.children = ( - widgets.Label("—", layout=widgets.Layout(margin="0 0 0 6px")), - ) + children.append(widgets.Label("—", layout=widgets.Layout(margin="0 0 0 6px"))) + control_box.children = tuple(children) poll_state = { "input_widget": input_widget, "reader": reader, "enum_opts": enum_opts, "dd_suppress": suppress_dd, + "row_state": row_state, } return control_box, poll_state -def _apply_item_update(entry): - """Read `entry["item"]`'s current value and refresh its row's widgets - (value label, enum-dropdown sync, state-class re-colouring) -- the - per-entry body _update_loop already runs for every active row each - tick, extracted so a standalone control (build_adjustable_control_widget) - can reuse the exact same update logic outside the assembly grid's own - item_entries/poll-loop bookkeeping. +def _apply_result(entry, ok, payload, monitor_state=None): + """Show one read of `entry["item"]` -- `payload` is the value, or (not + ok) why there is none: value label, enum-dropdown sync, state-class + re-colouring and the row's status dot. Called from reader threads + (widget values may be set from any thread).""" + row_state = entry.get("row_state") + if not ok: + if row_state is not None: + row_state.read_failed(payload) + return + val = payload + text = _format_value(val) + entry["value_widget"].value = text + # keep the dropdown selection tracking the readback (e.g. a valve + # settling from a stale/mismatched selection to its true OPEN/CLOSED + # state); dd_suppress stops this from looping back into the dropdown's + # change handler and issuing a spurious write. + enum_opts = entry.get("enum_opts") + dd_suppress = entry.get("dd_suppress") + if enum_opts and dd_suppress is not None and text in enum_opts: + dd = entry["input_widget"] + if dd.value != text: + dd_suppress[0] = True + try: + dd.value = text + finally: + dd_suppress[0] = False + # live green/red re-colouring -- open/closed and alarm severity can + # change between reads, so this must track the value, not just be set + # once at build time. + new_class = _state_css_class(entry["item"], val) + if new_class != entry.get("state_class"): + for cls in _STATE_CSS.values(): + entry["value_widget"].remove_class(cls) + if new_class: + entry["value_widget"].add_class(new_class) + entry["state_class"] = new_class + if row_state is not None: + row_state.read_ok(monitor_state) - Returns the elapsed read duration in seconds -- computed whether or - not the read raised, matching the existing scheduler's behaviour of - backing off a slow *failing* read the same as a slow successful one - (see _update_loop's own adaptive-interval comment).""" + +def _apply_item_update(entry): + """Read `entry["item"]`'s current value here and show it (see + _apply_result). Returns the elapsed read duration in seconds.""" t0 = time.monotonic() try: - val = entry["item"].get_current_value() - text = _format_value(val) - entry["value_widget"].value = text - # keep the dropdown selection tracking the readback (e.g. a valve - # settling from a stale/mismatched selection to its true - # OPEN/CLOSED state); dd_suppress (the same flag _enum_set uses - # after a real user-driven set) stops this from looping back into - # _on_dd_change and issuing a spurious write. - enum_opts = entry.get("enum_opts") - dd_suppress = entry.get("dd_suppress") - if enum_opts and dd_suppress is not None and text in enum_opts: - dd = entry["input_widget"] - if dd.value != text: - dd_suppress[0] = True - try: - dd.value = text - finally: - dd_suppress[0] = False - # live green/red re-colouring -- open/closed and alarm severity can - # change between polls, so this must track the value, not just be - # set once at build time. - new_class = _state_css_class(entry["item"], val) - if new_class != entry.get("state_class"): - for cls in _STATE_CSS.values(): - entry["value_widget"].remove_class(cls) - if new_class: - entry["value_widget"].add_class(new_class) - entry["state_class"] = new_class - except Exception: - pass + value = entry["item"].get_current_value() + if value is None: + _apply_result(entry, False, "no value (not connected?)") + else: + _apply_result(entry, True, value) + except Exception as exc: + try: + _apply_result(entry, False, live_values.describe(exc)) + except Exception: + pass return time.monotonic() - t0 def build_adjustable_control_widget(item, poll_interval=1.0, title=None): - """Standalone, self-polling version of one make_assembly_widget row + """Standalone, self-updating version of one make_assembly_widget row (name/value/controls) -- for use as a eco.widgets.containers.stack() child outside the normal assembly grid, e.g. via eco.widgets.containers.adjustable_control()/detector_indicator() (the same function serves both -- _build_item_controls_widget already - renders read-only for a plain, non-Adjustable Detector). Returns a - plain ipywidgets.HBox with its own `.stop()` attached directly -- no - wrapper object needed, since eco.widgets.widget_tray.teardown_widget - already calls `.stop()` if present before closing a widget's - subtree.""" + renders read-only for a plain, non-Adjustable Detector). Monitored where + the item supports it, polled otherwise, writes off the kernel thread -- + same rules as the grid. Returns a plain ipywidgets.HBox with its own + `.stop()` attached directly -- no wrapper object needed, since + eco.widgets.widget_tray.teardown_widget already calls `.stop()` if + present before closing a widget's subtree.""" name = title or ( item.alias.get_full_name() if hasattr(item, "alias") @@ -644,33 +645,17 @@ def build_adjustable_control_widget(item, poll_interval=1.0, title=None): control_box, poll_state = _build_item_controls_widget(item, value_w, cur) row = widgets.HBox([name_w, value_w, control_box], layout=_row_layout()) - entry = { - "item": item, - "value_widget": value_w, - "active": True, - "interval": poll_interval, - "next_due": 0.0, - "state_class": state_class, - **poll_state, - } + entry = {"item": item, "value_widget": value_w, "state_class": state_class, **poll_state} + reader = LiveReader( + lambda index, ok, payload: _apply_result(entry, ok, payload, reader.monitor_state(0)), + poll_interval=poll_interval, + workers=1, + ) + reader.add(item) + poll_state["row_state"].on_finished = lambda: reader.request_read(0) + reader.start() - stop_event = threading.Event() - - def _poll_loop(): - while not stop_event.is_set(): - duration = _apply_item_update(entry) - interval = min( - _POLL_MAX_INTERVAL, max(poll_interval, duration * _POLL_SLOWDOWN_FACTOR) - ) - if stop_event.wait(interval): - break - - threading.Thread(target=_poll_loop, daemon=True).start() - - def stop(): - stop_event.set() - - row.stop = stop + row.stop = reader.stop return row @@ -711,8 +696,30 @@ def make_assembly_widget( hidden_items = [it for it in all_items if it not in display_items] # read every initial value concurrently up front, so opening a large - # assembly is bounded by the slowest single readback, not their sum - prefetched = _prefetch_values(list(display_items) + hidden_items) + # assembly is bounded by the slowest single readback, not their sum -- + # and by PREFETCH_DEADLINE at most (a late row shows "connecting...") + prefetched = _prefetch_values( + list(display_items) + hidden_items, deadline=live_values.PREFETCH_DEADLINE + ) + + # results of the reads arrive here, from reader threads (see _apply_result) + def _on_result(index, ok, payload): + entry = item_entries[index] + if ok and entry.get("needs_controls"): + _build_late_controls(entry, payload) + _apply_result(entry, ok, payload, reader.monitor_state(index)) + + def _build_late_controls(entry, value): + """The first value of a row that had none at opening arrived: now its + controls (enum? numeric?) can be built.""" + entry["needs_controls"] = False + control_box, poll_state = _build_item_controls_widget( + entry["item"], entry["value_widget"], value, entry["row_state"] + ) + entry.update(poll_state) + entry["row"].children = (entry["row"].children[0], entry["value_widget"], control_box) + + reader = LiveReader(_on_result, poll_interval=poll_interval) header = widgets.HBox( [ @@ -783,6 +790,8 @@ def make_assembly_widget( except Exception: cur = "" + pending = cur is _PENDING + # child assemblies get a clickable name that expands/collapses their # own widget directly below this row; everything else is a plain label is_child_assembly = isinstance(item, Assembly) @@ -798,24 +807,34 @@ def make_assembly_widget( name_w.style.button_color = "#f5f5f5" else: name_w = widgets.Label(str(name), layout=_name_layout()) - value_w = widgets.Label(_format_value(cur), layout=_value_layout()) - state_class = _state_css_class(item, cur) + value_w = widgets.Label("…" if pending else _format_value(cur), layout=_value_layout()) + state_class = None if pending else _state_css_class(item, cur) if state_class: value_w.add_class(state_class) - control_box, poll_state = _build_item_controls_widget(item, value_w, cur) + row_state = _RowState(item) + if pending: + # no value yet, so no way to tell which controls fit (enum? + # numeric?): built when the first value arrives (_build_late_controls) + control_box = widgets.HBox( + [row_state.dot, widgets.Label("connecting…")], layout=_control_layout() + ) + poll_state = {"row_state": row_state} + else: + control_box, poll_state = _build_item_controls_widget(item, value_w, cur, row_state) row = widgets.HBox([name_w, value_w, control_box], layout=_row_layout()) if section_class: row.add_class(section_class) elements.append(row) + index = reader.add(item, active=active) # same index as in item_entries + row_state.on_finished = lambda i=index: reader.request_read(i) item_entries.append( { "item": item, "value_widget": value_w, - "active": active, - "interval": poll_interval, - "next_due": 0.0, # monotonic time; 0 => poll on first pass + "row": row, + "needs_controls": pending, "state_class": state_class, # seeded from the initial build **poll_state, } @@ -889,20 +908,13 @@ def make_assembly_widget( layout=widgets.Layout(width="150px"), ) - def _toggle_hidden(b, box=hidden_container, entries=hidden_entries): + def _toggle_hidden(b, box=hidden_container, start=hidden_entry_start, count=len(hidden_entries)): showing = box.layout.display != "none" - if showing: - box.layout.display = "none" - b.description = "expand hidden" - # stop polling collapsed items so they don't consume resources - for ent in entries: - ent["active"] = False - else: - box.layout.display = None - b.description = "hide hidden" - for ent in entries: - ent["active"] = True - ent["next_due"] = 0.0 # refresh immediately on expand + box.layout.display = "none" if showing else None + b.description = "expand hidden" if showing else "hide hidden" + # only read/monitor expanded items, so collapsed ones don't consume resources + for index in range(start, start + count): + reader.set_active(index, not showing) toggle_btn.on_click(_toggle_hidden) final_children.append(toggle_btn) @@ -933,45 +945,12 @@ def make_assembly_widget( vbox = widgets.VBox(final_children) - # background updater - stop_event = threading.Event() - updater_thread = None - - def _update_loop(): - # adaptive scheduler: poll each active item only when it is due, and - # back off items whose readback is slow so they don't stall the rest - while not stop_event.is_set(): - now = time.monotonic() - next_wakeup = now + poll_interval - for ent in item_entries: - if stop_event.is_set(): - break - if not ent.get("active", True): - continue - if now < ent["next_due"]: - next_wakeup = min(next_wakeup, ent["next_due"]) - continue - duration = _apply_item_update(ent) - ent["interval"] = min( - _POLL_MAX_INTERVAL, - max(poll_interval, duration * _POLL_SLOWDOWN_FACTOR), - ) - ent["next_due"] = time.monotonic() + ent["interval"] - next_wakeup = min(next_wakeup, ent["next_due"]) - sleep_for = max(0.05, next_wakeup - time.monotonic()) - if stop_event.wait(sleep_for): - break - + # reads and monitors run off the kernel thread (see live_values.LiveReader) def start(): - nonlocal updater_thread - if updater_thread and updater_thread.is_alive(): - return - stop_event.clear() - updater_thread = threading.Thread(target=_update_loop, daemon=True) - updater_thread.start() + reader.start() def stop(): - stop_event.set() + reader.stop() for state in child_states: child_widget = state.get("widget") if child_widget is not None: @@ -982,7 +961,7 @@ def make_assembly_widget( vbox.start = start vbox.stop = stop - vbox._stop_event = stop_event + vbox._reader = reader if auto_start: start() diff --git a/eco/widgets/indicator_widgets_qt_simple.py b/eco/widgets/indicator_widgets_qt_simple.py index b97de74..d586bd7 100644 --- a/eco/widgets/indicator_widgets_qt_simple.py +++ b/eco/widgets/indicator_widgets_qt_simple.py @@ -111,8 +111,18 @@ def _start_poll(widget, get_value, render, poll_interval, stop_event): the widget does -- a local-only QObject with a connected signal can otherwise be garbage-collected out from under the connection.""" bridge = _PollBridge() - bridge.updated.connect(render) + + def _render(value): + # a reading may arrive after the widget is gone: an exception in this + # slot would abort the whole process under PyQt -- stop reading instead + try: + render(value) + except RuntimeError: + stop_event.set() + + bridge.updated.connect(_render) widget._bridge = bridge + widget.destroyed.connect(lambda *args: stop_event.set()) def _loop(): while not stop_event.is_set(): diff --git a/eco/widgets/ioc_finder_qt.py b/eco/widgets/ioc_finder_qt.py index 5dcc45e..cd644b9 100644 --- a/eco/widgets/ioc_finder_qt.py +++ b/eco/widgets/ioc_finder_qt.py @@ -57,6 +57,36 @@ def _format_boot(m: IocMatch) -> str: return "\n".join(lines) +# Workers are deliberately not children of the widget: closing the window +# while one runs (a search, the 2 s console read) would destroy a running +# QThread with it, which aborts the whole process ("QThread: Destroyed while +# thread is still running"). They are kept alive here until they finish; their +# results go to bound methods of the widget, which Qt disconnects when the +# widget is deleted. +_running_workers = set() + + +def _guard(slot): + """A result handler for a worker that may finish after the window is + gone (only bound methods of the widget are disconnected automatically): + touching the deleted widgets raises RuntimeError, which in a Qt slot + would abort the process.""" + + def guarded(*args): + try: + slot(*args) + except RuntimeError: + pass + + return guarded + + +def _start_worker(worker): + _running_workers.add(worker) + worker.finished.connect(lambda w=worker: _running_workers.discard(w)) + worker.start() + + class _SearchWorker(QtCore.QThread): done = QtCore.Signal(list, str) @@ -205,9 +235,9 @@ class IocFinderQt(QtWidgets.QWidget): self.detail_text.setPlainText("") self.search_btn.setEnabled(False) - self._search_thread = _SearchWorker(pattern, parent=self) + self._search_thread = _SearchWorker(pattern) self._search_thread.done.connect(self._on_search_done) - self._search_thread.start() + _start_worker(self._search_thread) def _on_search_done(self, matches: list, error: str) -> None: self.search_btn.setEnabled(True) @@ -293,9 +323,9 @@ class IocFinderQt(QtWidgets.QWidget): m = self._selected self.console_status_label.setText("checking...") self.check_status_btn.setEnabled(False) - self._status_thread = _StatusWorker(m.console_host, m.console_port, parent=self) - self._status_thread.done.connect(lambda ok, m=m: self._on_status_done(ok, m)) - self._status_thread.start() + self._status_thread = _StatusWorker(m.console_host, m.console_port) + self._status_thread.done.connect(_guard(lambda ok, m=m: self._on_status_done(ok, m))) + _start_worker(self._status_thread) def _on_status_done(self, ok: bool, m: IocMatch) -> None: self.check_status_btn.setEnabled(True) @@ -324,7 +354,7 @@ class IocFinderQt(QtWidgets.QWidget): close_btn.clicked.connect(dlg.accept) dlg_layout.addWidget(close_btn) - self._output_thread = _ConsoleOutputWorker(m.console_host, m.console_port, parent=self) + self._output_thread = _ConsoleOutputWorker(m.console_host, m.console_port) def _done(out: str, error: str) -> None: self.output_btn.setEnabled(True) @@ -333,8 +363,8 @@ class IocFinderQt(QtWidgets.QWidget): else: text.setPlainText(out if out else "(no output received)") - self._output_thread.done.connect(_done) - self._output_thread.start() + self._output_thread.done.connect(_guard(_done)) + _start_worker(self._output_thread) dlg.exec_() # -- restart (gated by confirmation dialog) ----------------------------------- @@ -355,9 +385,9 @@ class IocFinderQt(QtWidgets.QWidget): return self.restart_btn.setEnabled(False) self.console_status_label.setText(f"sending restart to {m.console_host}:{m.console_port}...") - self._restart_thread = _RestartWorker(m.console_host, m.console_port, parent=self) - self._restart_thread.done.connect(lambda error, m=m: self._on_restart_done(error, m)) - self._restart_thread.start() + self._restart_thread = _RestartWorker(m.console_host, m.console_port) + self._restart_thread.done.connect(_guard(lambda error, m=m: self._on_restart_done(error, m))) + _start_worker(self._restart_thread) def _on_restart_done(self, error: str, m: IocMatch) -> None: self.restart_btn.setEnabled(True) diff --git a/eco/widgets/live_values.py b/eco/widgets/live_values.py new file mode 100644 index 0000000..85087b3 --- /dev/null +++ b/eco/widgets/live_values.py @@ -0,0 +1,482 @@ +"""Reading and writing eco items for a GUI without ever blocking it -- the +toolkit-independent part of the default property grids (Qt: display_qt, +ipywidgets/Voila: display_widget, Tk: display_tk). No GUI imports here; each +grid only decides how a result reaches its widgets (Qt: a timer flushing a +queue on the GUI thread; Tk: the same via ``after``; ipywidgets: directly, +since widget updates may come from any thread). + +- :class:`LiveReader` reads a set of items in a small thread pool: items that + can push updates (``add_value_callback`` -- motor records, SmarAct --, or + ``set_current_value_callback`` -- PV-backed adjustables/detectors, virtual + adjustables whose parents are) are *monitored*: a callback only requests a + re-read, coalesced to at most 1/MIN_READ_INTERVAL per item, so the shown + value always comes from ``get_current_value()`` (enum labels, conversions + exactly as when polled); polling stays as a slow safety net for them and is + the normal path for the rest, with adaptive back-off for slow items. One + slow or absent channel no longer holds up the others. + + A subscription alone proves nothing -- e.g. a virtual detector without + parents (vacuum valve readbacks, the prepump state) subscribes fine and + never calls back, PshellMotor calls back only when a move ends. So an item + counts as monitored only once an event has actually arrived, and is + demoted back to normal polling as soon as a scheduled poll finds a change + that no event announced. +- :func:`start_write` runs ``set_target_value`` and follows the move in a + background thread, reporting "moving" / done / the error (a blocked move's + own message -- e.g. eco.utilities.task_queue.MoveBlocked -- verbatim). +- :func:`prefetch_values` reads initial values concurrently with a deadline. +- :func:`initial_step` / :func:`load_ioc_step` / :func:`store_ioc_step`: the + tweak step -- the IOC's TWV for motor records with their own tweak fields + (eco.widgets.tweak_panel.has_ioc_tweak), else the adjustable's own tweak + step, else 1 / 0.1. +- ``STATE_COLORS``: the status-dot colours all grids use. +""" +import enum +import threading +import time +from concurrent.futures import ThreadPoolExecutor, wait as _wait_futures + +PREFETCH_DEADLINE = 2.0 # s: opening a grid waits at most this long for initial values +MIN_READ_INTERVAL = 0.1 # s: monitor-triggered re-reads of one item are coalesced to this +MONITORED_POLL_INTERVAL = 10.0 # s: safety-net polling of a monitored item +READ_WORKERS = 6 # parallel readbacks per grid +POLL_SLOWDOWN_FACTOR = 10.0 # a slow item is polled every ~duration * this ... +POLL_MAX_INTERVAL = 30.0 # ... at most this far apart + +PREFETCH_ERROR = object() # sentinel: a prefetched read raised +_NO_VALUE = object() # sentinel: not read yet +PENDING = object() # sentinel: no value yet (the prefetch deadline passed) + +# state -> (colour, explanation) of a row's status dot +STATE_COLORS = { + "waiting": ("#9e9e9e", "waiting for the first value (connecting)"), + "live": ("#2e9d4a", "connected, updated by monitor"), + "subscribed": ("#2e9d4a", "connected, monitored (no update received yet, also polled)"), + "polled": ("#3d7fd9", "connected, polled"), + "busy": ("#f0a020", "sending"), + "moving": ("#f0a020", "moving"), + "error": ("#d93025", "error"), +} + + +def format_value(value): + """Enum-valued readbacks by their label rather than the integer.""" + if isinstance(value, enum.Enum): + return value.name + return str(value) + + +def describe(exc): + """An exception as shown to the user: a blocked move's own explanation + (eco.utilities.task_queue.MoveBlocked, eco.elements.access.AccessDenied + -- both PermissionErrors) verbatim, else type and message.""" + if isinstance(exc, PermissionError): + return str(exc) + return f"{type(exc).__name__}: {exc}" + + +def dot_state(state, message, read_state): + """(colour, tooltip) for a status dot; state "idle" means "a write ended, + show what the reads say" (`read_state`, a (state, message) pair).""" + if state == "idle": + state, message = read_state + color, text = STATE_COLORS.get(state, STATE_COLORS["error"]) + return color, (f"{text}: {message}" if message else text) + + +# -- initial values --------------------------------------------------------------- + + +def prefetch_values(items, max_workers=8, deadline=None): + """Read all initial values concurrently, so building a grid is bounded + by the slowest single readback rather than their sum -- and by + `deadline` (s) at most: an item without a value by then maps to PENDING + (its read finishes in the background, unwaited). Returns {id(item): + value | PREFETCH_ERROR | PENDING}. Never raises.""" + values = {} + items = list(items) + if not items: + return values + + def _read(item): + return item.get_current_value() + + try: + executor = ThreadPoolExecutor(max_workers=min(max_workers, len(items))) + futures = {executor.submit(_read, item): item for item in items} + _wait_futures(futures, timeout=deadline) + for future, item in futures.items(): + if not future.done(): + values[id(item)] = PENDING + continue + try: + values[id(item)] = future.result() + except Exception: + values[id(item)] = PREFETCH_ERROR + executor.shutdown(wait=False) + except Exception: + pass + return values + + +# -- monitoring and polling ----------------------------------------------------------- + + +def subscribe(item, kick): + """Have `item` call kick() whenever its value changes, if it can push + updates at all. Returns an unsubscribe callable, or None. May wait for a + channel connection: call it off the GUI thread.""" + + def _on_update(*args, **kwargs): + kick() + + if callable(getattr(item, "add_value_callback", None)): + try: + index = item.add_value_callback(_on_update) + except Exception: + index = None + else: + clear = getattr(item, "clear_value_callback", None) + + def _unsubscribe(): + if callable(clear) and index is not None: + clear(index) + + return _unsubscribe + if callable(getattr(item, "set_current_value_callback", None)): + try: + monitor = item.set_current_value_callback(func=_on_update) + if monitor is None or not callable(getattr(monitor, "start", None)): + return None + if getattr(monitor, "children", None) == []: + return None # a composed value of nothing: it would never call back + try: + monitor.start(add_current_value=False, with_ctrlvars=False) + except TypeError: + monitor.start() + return monitor.stop + except Exception: + return None + return None + + +class LiveReader: + """Keeps a set of items' values coming, never on the caller's thread. + + ``add(item)`` registers an item and returns its index; results arrive + through ``on_result(index, ok, value_or_message)``, called from pool + threads -- marshal to the GUI thread there if the toolkit needs it. A + read returning None is reported as "no value (not connected?)", which is + how a failed CA get comes back. + """ + + def __init__(self, on_result, poll_interval=1.0, workers=READ_WORKERS): + self.on_result = on_result + self.poll_interval = poll_interval + self.workers = workers + self.entries = [] + self._wake = threading.Event() + self._stop = threading.Event() + self._executor = None + self._thread = None + + def add(self, item, active=True): + self.entries.append( + { + "item": item, + "active": active, + "interval": self.poll_interval, + "next_due": 0.0, # monotonic time; 0 => read on the first pass + "last_read": 0.0, + "in_flight": False, + "kicked": False, + "kicked_read": False, # whether the read in flight was asked for (monitor/write) + "monitored": False, # subscribed, an event arrived, not demoted -- see monitored() + "events": 0, + "demoted": False, + "last_value": _NO_VALUE, + "subscribed": False, + "unsubscribe": None, + } + ) + return len(self.entries) - 1 + + def monitored(self, index): + """Whether item `index` is kept current by its monitor (an event has + arrived, and no poll has caught it missing a change since).""" + return self.entries[index]["monitored"] + + def monitor_state(self, index): + """"live" (its monitor reports changes), "subscribed" (monitored but + nothing reported yet -- e.g. a motor at rest; still polled), or None + (polled: no monitor, or one that missed a change).""" + entry = self.entries[index] + if entry["monitored"]: + return "live" + if entry["unsubscribe"] is not None and not entry["demoted"]: + return "subscribed" + return None + + def _on_event(self, index): + """A monitor callback for item `index` (any thread).""" + entry = self.entries[index] + entry["events"] += 1 + if entry["unsubscribe"] is not None and not entry["demoted"]: + entry["monitored"] = True + self.request_read(index) + + def set_active(self, index, active): + """Only active items are read and monitored (e.g. collapsed hidden + rows are not); activating one reads it at once.""" + entry = self.entries[index] + entry["active"] = active + if active: + entry["next_due"] = 0.0 + self._wake.set() + + def request_read(self, index): + """Read item `index` as soon as possible (a monitor saw a change, a + write finished) -- at most every MIN_READ_INTERVAL. Any thread.""" + try: + entry = self.entries[index] + except IndexError: + return + entry["kicked"] = True # also covers a change during a read in flight, see _read + if not entry["in_flight"]: + entry["next_due"] = entry["last_read"] + MIN_READ_INTERVAL + self._wake.set() + + def start(self): + if self._thread is not None: + return + self._stop.clear() + self._executor = ThreadPoolExecutor(max_workers=self.workers) + self._thread = threading.Thread(target=self._schedule, daemon=True) + self._thread.start() + + def stop(self): + """Stop reading; monitors are removed in a background thread + (removing a CA callback can wait on the CA library).""" + self._stop.set() + self._wake.set() + self._thread = None + if self._executor is not None: + self._executor.shutdown(wait=False, cancel_futures=True) + self._executor = None + unsubscribes = [e["unsubscribe"] for e in self.entries if e["unsubscribe"]] + for entry in self.entries: # so that start() again begins afresh + entry.update(unsubscribe=None, subscribed=False, monitored=False, in_flight=False, + next_due=0.0, events=0, demoted=False) + if unsubscribes: + + def _unsubscribe_all(): + for unsubscribe in unsubscribes: + try: + unsubscribe() + except Exception: + pass + + threading.Thread(target=_unsubscribe_all, daemon=True).start() + + # -- internals --------------------------------------------------------------- + + def _subscribe(self, index): + entry = self.entries[index] + unsubscribe = subscribe(entry["item"], lambda i=index: self._on_event(i)) + if self._stop.is_set(): # closed while subscribing + if unsubscribe is not None: + try: + unsubscribe() + except Exception: + pass + return + entry["unsubscribe"] = unsubscribe # monitored only once an event arrives, see _on_event + + def _read(self, index): + entry = self.entries[index] + t0 = time.monotonic() + try: + value = entry["item"].get_current_value() + if value is None: + result = (False, "no value (not connected?)") + else: + result = (True, value) + except Exception as exc: + result = (False, describe(exc)) + now = time.monotonic() + if result[0] and entry["monitored"] and not entry["kicked_read"]: + # a scheduled safety poll: a change no event announced means the + # monitor doesn't report this item's changes -- poll it normally + if _changed(entry["last_value"], result[1]): + entry["monitored"] = False + entry["demoted"] = True + if result[0]: + entry["last_value"] = result[1] + if entry["monitored"]: + interval = MONITORED_POLL_INTERVAL + else: + interval = min(POLL_MAX_INTERVAL, max(self.poll_interval, (now - t0) * POLL_SLOWDOWN_FACTOR)) + entry["interval"] = interval + entry["last_read"] = now + # a change reported while this read ran may not be in it: read again soon + entry["next_due"] = now + (MIN_READ_INTERVAL if entry["kicked"] else interval) + entry["kicked"] = False + entry["in_flight"] = False + self._wake.set() + if not self._stop.is_set(): + try: + self.on_result(index, *result) + except Exception: + pass + + def _schedule(self): + # hands due reads to the pool (never more than one in flight per + # item) and subscribes monitorable items once they are active + while not self._stop.is_set(): + now = time.monotonic() + next_wakeup = now + max(self.poll_interval, 0.05) + for index, entry in enumerate(self.entries): + if self._stop.is_set(): + return + if not entry["active"]: + continue + executor = self._executor + if executor is None: + return + try: + if not entry["subscribed"]: + entry["subscribed"] = True + executor.submit(self._subscribe, index) + if entry["in_flight"]: + continue + if now < entry["next_due"]: + next_wakeup = min(next_wakeup, entry["next_due"]) + continue + entry["in_flight"] = True + entry["kicked_read"] = entry["kicked"] or entry["next_due"] == 0.0 + entry["kicked"] = False # this read covers what was reported so far + executor.submit(self._read, index) + except RuntimeError: # the pool was shut down: stopping + return + self._wake.wait(max(0.02, next_wakeup - time.monotonic())) + self._wake.clear() + + +def _changed(old, new): + if old is _NO_VALUE: + return False + try: + return bool(old != new) + except Exception: # e.g. arrays: undecidable, so no verdict + return False + + +# -- writes ------------------------------------------------------------------------------- + + +def start_write(item, target_fn, on_state, on_finished=None, holder=None): + """``item.set_target_value(target_fn())`` in a background thread, + following the move. `target_fn` runs there too (it may read the item, + e.g. current value + step). Reports through ``on_state(state, message)`` + from that thread: "moving" once a changer exists, then "idle" (done) or + "error" with :func:`describe`'s text. ``on_finished()`` always runs at + the end (e.g. to request a re-read); ``holder["changer"]`` is the + changer, for :func:`stop_move`.""" + + def run(): + try: + target = target_fn() + changer = item.set_target_value(target) + if holder is not None: + holder["changer"] = changer + if hasattr(changer, "wait"): + on_state("moving", f"to {target!r}") + changer.wait() + on_state("idle", "") + except Exception as exc: + on_state("error", describe(exc)) + finally: + if on_finished is not None: + try: + on_finished() + except Exception: + pass + + threading.Thread(target=run, daemon=True).start() + + +def stop_move(item, changer, on_state): + """Stop the move `changer` belongs to (or `item` itself), in a + background thread.""" + + def run(): + try: + if changer is not None and callable(getattr(changer, "stop", None)): + changer.stop() + elif callable(getattr(item, "stop", None)): + item.stop() + except Exception as exc: + on_state("error", f"stop failed: {describe(exc)}") + + threading.Thread(target=run, daemon=True).start() + + +# -- tweak step ------------------------------------------------------------------------------ + + +def default_step_for(value): + if isinstance(value, int) and not isinstance(value, bool): + return 1 + return 0.1 + + +def initial_step(item, value): + """The step a tweak field starts with before (or without) the IOC's: the + adjustable's own tweak step if one was set up, else 1 / 0.1.""" + try: + from eco.manual_control.step_store import tweak_step_size + + step = tweak_step_size(item) + if step: + return step + except Exception: + pass + return default_step_for(value) + + +def has_ioc_step(item): + """Whether `item` is a motor record with its own tweak step (TWV).""" + try: + from eco.widgets.tweak_panel import has_ioc_tweak + + return has_ioc_tweak(item) + except Exception: + return False + + +def load_ioc_step(item, on_step): + """Read the IOC's tweak step (TWV) in a background thread and pass it to + ``on_step(value: float)``. Returns a holder whose "pv" is the channel + once known (for :func:`store_ioc_step`).""" + holder = {} + + def run(): + try: + from eco.widgets.tweak_panel import ioc_tweak_pvs + + pv = holder["pv"] = ioc_tweak_pvs(item)[0] + value = pv.get(timeout=2.0) + if value is not None: + on_step(float(value)) + except Exception: + pass + + threading.Thread(target=run, daemon=True).start() + return holder + + +def store_ioc_step(holder, value): + """Write a tweak step back to the IOC (TWV), in a background thread.""" + pv = holder.get("pv") + if pv is not None: + threading.Thread(target=lambda: pv.put(float(value)), daemon=True).start() diff --git a/eco/widgets/tweak_panel.py b/eco/widgets/tweak_panel.py index 8320a1e..3f3ab83 100644 --- a/eco/widgets/tweak_panel.py +++ b/eco/widgets/tweak_panel.py @@ -157,21 +157,30 @@ class SoftTweakAxis(_AxisBase): self._record_target(value) +def has_ioc_tweak(obj): + """Whether `obj` is a motor record with its own tweak fields (TWV/TWF/ + TWR) -- the classes that implement ``_tweak_ioc`` (MotorRecord, + SmaractRecord, SmaractStreamdevice, ...).""" + return hasattr(obj, "_tweak_ioc") + + +def ioc_tweak_pvs(motor): + """(step TWV, forward TWF, reverse TWR) channels of a motor record -- + the record's own fields if eco wraps it as an eco_epics Motor, else the + ``:TWV``-style records of the SmarAct IOCs.""" + if hasattr(motor, "_motor") and hasattr(motor._motor, "get_pv"): + return motor._motor.get_pv("TWV"), motor._motor.get_pv("TWF"), motor._motor.get_pv("TWR") + from epics import PV + + return PV(motor.pvname + ":TWV"), PV(motor.pvname + ":TWF.PROC"), PV(motor.pvname + ":TWR.PROC") + + class IocTweakAxis(_AxisBase): """A motor record (MotorRecord, SmaractRecord, SmaractStreamdevice, ...) tweaked through its own TWV/TWF/TWR fields.""" def __init__(self, motor, step=None, go_to_current_value_first=False): - if hasattr(motor, "_motor") and hasattr(motor._motor, "get_pv"): - self._pv_step = motor._motor.get_pv("TWV") - self._pv_fwd = motor._motor.get_pv("TWF") - self._pv_rev = motor._motor.get_pv("TWR") - else: - from epics import PV - - self._pv_step = PV(motor.pvname + ":TWV") - self._pv_fwd = PV(motor.pvname + ":TWF.PROC") - self._pv_rev = PV(motor.pvname + ":TWR.PROC") + self._pv_step, self._pv_fwd, self._pv_rev = ioc_tweak_pvs(motor) if not step: step = self._pv_step.get() try: diff --git a/eco/widgets/tweak_panel_qt.py b/eco/widgets/tweak_panel_qt.py index c657823..095adca 100644 --- a/eco/widgets/tweak_panel_qt.py +++ b/eco/widgets/tweak_panel_qt.py @@ -331,7 +331,17 @@ class TweakPanelQt(QtWidgets.QFrame): def _show_message(self, text): self.status.setText(text) - QtCore.QTimer.singleShot(8000, lambda: self.status.setText("")) + + def _clear(): + # only this message (not a newer one), and only while the panel + # exists: an exception in a timer callback aborts under PyQt + try: + if self.status.text() == text: + self.status.setText("") + except RuntimeError: + pass + + QtCore.QTimer.singleShot(8000, _clear) def closeEvent(self, event): self._stop_event.set() diff --git a/tests/test_containers.py b/tests/test_containers.py index 19d536b..44a5761 100644 --- a/tests/test_containers.py +++ b/tests/test_containers.py @@ -286,9 +286,18 @@ def test_build_item_controls_qt_adjustable_stop_reset(): stop_btn = next(b for b in buttons if b.text() == "🛑") reset_btn = next(b for b in buttons if b.text() == "↺") + # writes run off the GUI thread now (see display_qt's module docstring) + def wait_for_calls(n): + deadline = time.monotonic() + 2.0 + while len(item.set_calls) < n and time.monotonic() < deadline: + app.processEvents() + time.sleep(0.01) + up_btn.click() + wait_for_calls(1) assert item.set_calls == [2] # base 1 + step 1 reset_btn.click() + wait_for_calls(2) assert item.set_calls[-1] == 1 # back to the value the control opened with stop_btn.click() # no changer with .stop() attached here -- just shouldn't raise diff --git a/tests/test_display_qt.py b/tests/test_display_qt.py new file mode 100644 index 0000000..2e3bf18 --- /dev/null +++ b/tests/test_display_qt.py @@ -0,0 +1,228 @@ +"""Tests for eco.widgets.display_qt's responsiveness rules: writes off the +GUI thread with a status dot, monitored rows, parallel/bounded reads, and the +tweak step from the IOC.""" + +import threading +import time + +import pytest + +pytest.importorskip("qtpy") + +from qtpy import QtCore, QtWidgets + +from eco.widgets import display_qt + + +@pytest.fixture +def app(): + return QtWidgets.QApplication.instance() or QtWidgets.QApplication([]) + + +def pump(app, seconds, until=None): + deadline = time.monotonic() + seconds + while time.monotonic() < deadline: + app.processEvents() + if until is not None and until(): + return True + time.sleep(0.01) + return until() if until is not None else None + + +class _Changer: + def __init__(self, duration): + self.done = threading.Event() + self.stopped = False + self._duration = duration + + def wait(self): + self.done.wait(self._duration) + + def stop(self): + self.stopped = True + self.done.set() + + +class SlowAdjustable: + name = "slow" + + def __init__(self, value=1.0, duration=1.0, error=None): + self.value = value + self.duration = duration + self.error = error + self.targets = [] + self.changer = None + + def get_current_value(self): + return self.value + + def set_target_value(self, value): + if self.error is not None: + raise self.error + self.targets.append(value) + self.changer = _Changer(self.duration) + return self.changer + + +def dot_of(controls): + return controls.controller.dot + + +def test_a_click_returns_at_once_and_the_row_shows_the_move(app): + item = SlowAdjustable(duration=5.0) + controls, _ = display_qt._build_item_controls_qt(item, QtWidgets.QLabel("1.0"), 1.0) + up = next(b for b in controls.findChildren(QtWidgets.QPushButton) if b.text() == "▲") + stop = next(b for b in controls.findChildren(QtWidgets.QPushButton) if b.text() == "🛑") + t0 = time.monotonic() + up.click() + assert time.monotonic() - t0 < 0.2 # used to block for up to 5 s + assert pump(app, 2, until=lambda: dot_of(controls).toolTip().startswith("moving")) + assert item.targets == [pytest.approx(1.1)] + stop.click() # clickable during the move + assert pump(app, 2, until=lambda: item.changer.stopped) + assert pump(app, 2, until=lambda: not dot_of(controls).toolTip().startswith("moving")) + + +@pytest.mark.parametrize( + "error, shown", + [ + (PermissionError("mot is in use by queue 'default', task #1 -- not moved."), "in use by queue"), + (RuntimeError("limit switch"), "RuntimeError: limit switch"), + ], +) +def test_a_failed_or_blocked_write_shows_red_with_the_reason(app, error, shown): + item = SlowAdjustable(error=error) + controls, _ = display_qt._build_item_controls_qt(item, QtWidgets.QLabel("1.0"), 1.0) + entry = next(e for e in controls.findChildren(QtWidgets.QLineEdit) if e.text() == "1.0") + entry.setText("2.0") + entry.editingFinished.emit() + assert pump(app, 2, until=lambda: shown in dot_of(controls).toolTip()) + assert "#d93025" in dot_of(controls).styleSheet() + + +class FakePV: + def __init__(self, value): + self.value = value + self.puts = [] + + def get(self, timeout=None): + return self.value + + def put(self, value): + self.puts.append(value) + + +class FakeMotorRecord(SlowAdjustable): + """Has the IOC tweak fields, like MotorRecord (see tweak_panel.has_ioc_tweak).""" + + def __init__(self): + super().__init__(value=3.0, duration=0.0) + self.twv = FakePV(0.25) + self._motor = type("M", (), {"get_pv": lambda _, field: {"TWV": self.twv}.get(field, FakePV(0))})() + + def _tweak_ioc(self): + pass + + +def test_the_tweak_step_comes_from_the_ioc_and_edits_go_back(app): + item = FakeMotorRecord() + controls, _ = display_qt._build_item_controls_qt(item, QtWidgets.QLabel("3.0"), 3.0) + step = controls.findChildren(QtWidgets.QLineEdit)[0] + assert pump(app, 2, until=lambda: step.text() == "0.25") + step.setText("0.5") + step.editingFinished.emit() + assert pump(app, 2, until=lambda: item.twv.puts == [0.5]) + + +def test_the_tweak_step_falls_back_to_the_adjustables_own_tweak(app): + item = SlowAdjustable(value=3.0) + item._tweak_instance = type("T", (), {"step_sizes": [0.05]})() + controls, _ = display_qt._build_item_controls_qt(item, QtWidgets.QLabel("3.0"), 3.0) + assert controls.findChildren(QtWidgets.QLineEdit)[0].text() == "0.05" + + +# -- the whole window -------------------------------------------------------------- + + +class MonitoredAdjustable(SlowAdjustable): + """Pushes updates like a motor record (add_value_callback).""" + + def __init__(self, name): + super().__init__(value=0.0, duration=0.0) + self.name = name + self.callbacks = {} + + def add_value_callback(self, callback, index=None): + index = len(self.callbacks) + 1 + self.callbacks[index] = callback + return index + + def clear_value_callback(self, index=None): + self.callbacks.pop(index, None) + + def push(self, value): + self.value = value + for callback in list(self.callbacks.values()): + callback(pvname=self.name, value=value) + + +class HangingAdjustable(SlowAdjustable): + """Answers only after `delay` (a channel connecting slowly).""" + + def __init__(self, name, delay): + super().__init__(value=7.0) + self.name = name + self.delay = delay + + def get_current_value(self): + time.sleep(self.delay) + return self.value + + +class DeadDetector: + name = "dead" + + def get_current_value(self): + return None # how a failed CA read comes back + + +class FakeAssembly: + name = "fake" + + def __init__(self, items): + self.items = items + self.status_collection = type("SC", (), {"get_list": lambda _, selection=None: list(items)})() + + def display_collection(self): + return list(self.items) + + +def test_window_opens_fast_monitors_and_reads_in_parallel(app, monkeypatch): + from eco.widgets import live_values + + monkeypatch.setattr(live_values, "PREFETCH_DEADLINE", 0.5) + monitored = MonitoredAdjustable("monitored") + slow = HangingAdjustable("slow", delay=1.5) + dead = DeadDetector() + assembly = FakeAssembly([monitored, slow, dead]) + t0 = time.monotonic() + gui = display_qt.DisplayQt(assembly, poll_interval=30.0, auto_start=False) + gui._build_window() + try: + assert time.monotonic() - t0 < 1.5 # did not wait for the slow row + rows = {e["item"].name: e for e in gui._entries} + assert rows["slow"]["value_label"].text() == "…" + # monitored: subscribed, and a push shows at once despite the 30 s poll interval + assert pump(app, 3, until=lambda: monitored.callbacks) + monitored.push(4.5) + assert pump(app, 2, until=lambda: rows["monitored"]["value_label"].text() == "4.5") + assert "updated by monitor" in rows["monitored"]["controller"].dot.toolTip() + # the slow row gets its value and then its controls + assert pump(app, 4, until=lambda: rows["slow"]["value_label"].text() == "7.0") + assert not rows["slow"]["needs_controls"] + assert any(b.text() == "▲" for b in rows["slow"]["controls"].findChildren(QtWidgets.QPushButton)) + # a channel that returns no value is shown as such + assert pump(app, 2, until=lambda: "not connected" in rows["dead"]["controller"].dot.toolTip()) + finally: + gui.stop() + assert pump(app, 2, until=lambda: not monitored.callbacks) # unsubscribed on close diff --git a/tests/test_display_tk.py b/tests/test_display_tk.py new file mode 100644 index 0000000..148ccbe --- /dev/null +++ b/tests/test_display_tk.py @@ -0,0 +1,134 @@ +"""Tests for eco.widgets.display_tk (the Tk fallback property grid): the Tk +thread never reads or writes hardware, monitored rows, rows still connecting +at opening. Needs a display (skipped without one).""" + +import threading +import time + +import pytest + +tk = pytest.importorskip("tkinter") + +from eco.widgets import display_tk, live_values + + +@pytest.fixture +def root(): + try: + r = tk.Tk() + except tk.TclError: + pytest.skip("no display") + r.withdraw() + yield r + try: + r.destroy() + except tk.TclError: + pass + + +def pump(root, seconds, until): + deadline = time.monotonic() + seconds + while time.monotonic() < deadline: + root.update() + if until(): + return True + time.sleep(0.01) + return until() + + +class Changer: + def __init__(self, seconds): + self.done = threading.Event() + self.seconds = seconds + self.stopped = False + + def wait(self): + self.done.wait(self.seconds) + + def stop(self): + self.stopped = True + self.done.set() + + +class Adjustable: + def __init__(self, name, value=1.0, duration=0.0, delay=0.0): + self.name = name + self.value = value + self.duration = duration + self.delay = delay + self.targets = [] + self.changer = None + self.callbacks = {} + + def get_current_value(self): + time.sleep(self.delay) + return self.value + + def set_target_value(self, value): + self.targets.append(value) + self.changer = Changer(self.duration) + return self.changer + + def add_value_callback(self, callback, index=None): + index = len(self.callbacks) + 1 + self.callbacks[index] = callback + return index + + def clear_value_callback(self, index=None): + self.callbacks.pop(index, None) + + +class FakeAssembly: + name = "fake" + + def __init__(self, items): + self.items = items + self.status_collection = type("SC", (), {"get_list": lambda _, selection=None: list(items)})() + + def display_collection(self): + return list(self.items) + + +def find_buttons(widget, text): + found = [] + for child in widget.winfo_children(): + try: + if child.cget("text") == text: + found.append(child) + except tk.TclError: + pass + found += find_buttons(child, text) + return found + + +def test_tk_grid_never_blocks_and_monitors(root, monkeypatch): + monkeypatch.setattr(live_values, "PREFETCH_DEADLINE", 0.5) + mot = Adjustable("mot", duration=5.0) + slow = Adjustable("slow", value=7.0, delay=1.5) + gui = display_tk.DisplayTk(FakeAssembly([mot, slow]), poll_interval=30.0, auto_start=False) + t0 = time.monotonic() + gui._build_window(tk.Toplevel(root)) + try: + assert time.monotonic() - t0 < 1.5 + entries = {e["item"].name: e for e in gui._entries} + assert entries["slow"]["value_var"].get() == "…" + # a click returns at once; the dot shows the move; Stop works meanwhile + up = find_buttons(entries["mot"]["control"], "▲")[0] + t0 = time.monotonic() + up.invoke() + assert time.monotonic() - t0 < 0.2 + assert pump(root, 2, lambda: entries["mot"]["state"].tooltip.startswith("moving")) + find_buttons(entries["mot"]["control"], "\U0001f6d1")[0].invoke() + assert pump(root, 2, lambda: mot.changer.stopped) + # monitored: a push shows at once although the poll interval is 30 s + assert pump(root, 2, lambda: mot.callbacks) + mot.value = 2.5 + for callback in list(mot.callbacks.values()): + callback(pvname="mot", value=2.5) + assert pump(root, 1, lambda: entries["mot"]["value_var"].get() == "2.5") + # the slow row gets its value, then its controls + assert pump(root, 4, lambda: entries["slow"]["value_var"].get() == "7.0") + assert find_buttons(entries["slow"]["control"], "▲") + finally: + gui.stop() + assert pump(root, 2, lambda: not mot.callbacks) diff --git a/tests/test_display_widget.py b/tests/test_display_widget.py new file mode 100644 index 0000000..39f45d3 --- /dev/null +++ b/tests/test_display_widget.py @@ -0,0 +1,195 @@ +"""Tests for eco.widgets.display_widget (the ipywidgets/lab/Voila property +grid): writes off the kernel thread with a status dot, monitored rows, the +IOC tweak step, rows still connecting at opening.""" + +import threading +import time + +import pytest + +widgets = pytest.importorskip("ipywidgets") + +from eco.widgets import display_widget, live_values + + +def wait_for(condition, seconds=3.0): + deadline = time.monotonic() + seconds + while time.monotonic() < deadline: + if condition(): + return True + time.sleep(0.01) + return condition() + + +class Changer: + def __init__(self, seconds): + self.done = threading.Event() + self.seconds = seconds + self.stopped = False + + def wait(self): + self.done.wait(self.seconds) + + def stop(self): + self.stopped = True + self.done.set() + + +class Adjustable: + def __init__(self, name="a", value=1.0, duration=0.0, error=None): + self.name = name + self.value = value + self.duration = duration + self.error = error + self.targets = [] + self.changer = None + + def get_current_value(self): + return self.value + + def set_target_value(self, value): + if self.error is not None: + raise self.error + self.targets.append(value) + self.value = value + self.changer = Changer(self.duration) + return self.changer + + +def buttons(box): + found = [] + + def walk(w): + if isinstance(w, widgets.Button): + found.append(w) + for child in getattr(w, "children", ()): + walk(child) + + walk(box) + return found + + +def tooltip(row_state): + return row_state.dot.value + + +def test_a_click_returns_at_once_and_stop_works_during_the_move(): + item = Adjustable(duration=5.0) + box, state = display_widget._build_item_controls_widget(item, widgets.Label("1.0"), 1.0) + up = next(b for b in buttons(box) if b.description == "▲") + stop = next(b for b in buttons(box) if b.description == "🛑") + t0 = time.monotonic() + up.click() + assert time.monotonic() - t0 < 0.2 # used to block the kernel for up to 5 s + assert wait_for(lambda: "moving" in tooltip(state["row_state"])) + assert item.targets == [pytest.approx(1.1)] + stop.click() + assert wait_for(lambda: item.changer.stopped) + assert wait_for(lambda: "moving" not in tooltip(state["row_state"])) + + +def test_a_blocked_write_shows_red_with_the_reason(): + item = Adjustable(error=PermissionError("mot is in use by queue 'default', task #1 -- not moved.")) + box, state = display_widget._build_item_controls_widget(item, widgets.Label("1.0"), 1.0) + state["input_widget"].value = 2.0 + assert wait_for(lambda: "in use by queue" in tooltip(state["row_state"])) + assert "#d93025" in tooltip(state["row_state"]) + + +class FakePV: + def __init__(self, value): + self.value = value + self.puts = [] + + def get(self, timeout=None): + return self.value + + def put(self, value): + self.puts.append(value) + + +class MotorRecord(Adjustable): + def __init__(self): + super().__init__(value=3.0) + self.twv = FakePV(0.25) + self._motor = type("M", (), {"get_pv": lambda _, field: self.twv})() + + def _tweak_ioc(self): + pass + + +def test_the_tweak_step_comes_from_the_ioc_and_edits_go_back(): + item = MotorRecord() + box, state = display_widget._build_item_controls_widget(item, widgets.Label("3.0"), 3.0) + step = next(w for w in box.children if isinstance(w, widgets.FloatText) and w is not state["input_widget"]) + assert wait_for(lambda: step.value == 0.25) + assert item.twv.puts == [] # loading it did not write it back + step.value = 0.5 + assert wait_for(lambda: item.twv.puts == [0.5]) + + +class Monitored(Adjustable): + def __init__(self, name): + super().__init__(name=name, value=0.0) + self.callbacks = {} + + def add_value_callback(self, callback, index=None): + index = len(self.callbacks) + 1 + self.callbacks[index] = callback + return index + + def clear_value_callback(self, index=None): + self.callbacks.pop(index, None) + + def push(self, value): + self.value = value + for callback in list(self.callbacks.values()): + callback(pvname=self.name, value=value) + + +class Hanging(Adjustable): + def __init__(self, name, delay): + super().__init__(name=name, value=7.0) + self.delay = delay + + def get_current_value(self): + time.sleep(self.delay) + return self.value + + +class FakeAssembly: + name = "fake" + + def __init__(self, items): + self.items = items + self.status_collection = type("SC", (), {"get_list": lambda _, selection=None: list(items)})() + + def display_collection(self): + return list(self.items) + + +def test_grid_opens_fast_monitors_and_builds_late_controls(monkeypatch): + monkeypatch.setattr(live_values, "PREFETCH_DEADLINE", 0.5) + monitored = Monitored("monitored") + slow = Hanging("slow", delay=1.5) + t0 = time.monotonic() + grid = display_widget.make_assembly_widget(FakeAssembly([monitored, slow]), poll_interval=30.0) + try: + assert time.monotonic() - t0 < 1.5 + rows = [w for w in grid.children if isinstance(w, widgets.HBox) and len(w.children) == 3] + slow_row = next(r for r in rows if r.children[0].value == "slow") + monitored_row = next(r for r in rows if r.children[0].value == "monitored") + assert slow_row.children[1].value == "…" + # monitored: a push shows at once although the poll interval is 30 s + assert wait_for(lambda: monitored.callbacks) + monitored.push(4.5) + assert wait_for(lambda: monitored_row.children[1].value == "4.5", 1.0) + assert "updated by monitor" in monitored_row.children[2].children[0].value + # the slow row gets its value, then its controls + assert wait_for(lambda: slow_row.children[1].value == "7.0", 4.0) + assert any(b.description == "▲" for b in buttons(slow_row.children[2])) + # and the main ("kernel") thread was free the whole time: updates + # reach the widgets from the reader threads, as in Voila + finally: + grid.stop() + assert wait_for(lambda: not monitored.callbacks) diff --git a/tests/test_indicator_widgets_qt_simple_lifecycle.py b/tests/test_indicator_widgets_qt_simple_lifecycle.py new file mode 100644 index 0000000..5cb7790 --- /dev/null +++ b/tests/test_indicator_widgets_qt_simple_lifecycle.py @@ -0,0 +1,38 @@ +"""A simple Qt gadget deleted while its poll thread keeps reading must stop +quietly -- a reading rendered into the deleted widget used to raise inside a +Qt slot, which aborts the process under PyQt.""" + +import time + +import pytest + +pytest.importorskip("qtpy") + +from qtpy import QtCore, QtWidgets + +from eco.widgets import indicator_widgets_qt_simple as gadgets + + +class Item: + name = "x" + reads = 0 + + def get_current_value(self): + Item.reads += 1 + return 1.0 + + +def test_a_deleted_gadget_stops_reading_instead_of_aborting(): + app = QtWidgets.QApplication.instance() or QtWidgets.QApplication([]) + box = gadgets.bar_gauge(Item(), poll_interval=0.05) + for _ in range(10): + app.processEvents() + time.sleep(0.02) + box.deleteLater() + QtCore.QCoreApplication.sendPostedEvents(None, QtCore.QEvent.DeferredDelete) + for _ in range(20): # readings arriving after the deletion + app.processEvents() + time.sleep(0.02) + reads = Item.reads + time.sleep(0.3) + assert Item.reads <= reads + 1 # the poll thread stopped diff --git a/tests/test_ioc_finder_qt_lifecycle.py b/tests/test_ioc_finder_qt_lifecycle.py new file mode 100644 index 0000000..76797e5 --- /dev/null +++ b/tests/test_ioc_finder_qt_lifecycle.py @@ -0,0 +1,44 @@ +"""Closing the IOC finder while a worker runs must not abort the process +(it used to destroy the running QThread with the widget).""" + +import threading +import time + +import pytest + +pytest.importorskip("qtpy") + +from qtpy import QtCore, QtWidgets + +from eco.widgets import ioc_finder_qt + + +def test_closing_the_window_while_a_search_runs_does_not_abort(monkeypatch): + app = QtWidgets.QApplication.instance() or QtWidgets.QApplication([]) + release = threading.Event() + + def slow_find(pattern): + release.wait(5) + return [] + + monkeypatch.setattr(ioc_finder_qt, "find_ioc", slow_find, raising=False) + finder = ioc_finder_qt.IocFinderQt() + finder.search_box.setText("SARES20") + finder.search() + worker = finder._search_thread + assert worker.isRunning() + finder.setAttribute(QtCore.Qt.WA_DeleteOnClose, True) + finder.close() + finder.deleteLater() + # really delete it now (processEvents alone doesn't run deferred + # deletions) -- with the old code this aborted the process + QtCore.QCoreApplication.sendPostedEvents(None, QtCore.QEvent.DeferredDelete) + for _ in range(20): + app.processEvents() + time.sleep(0.01) + release.set() # the worker finishes after its widget is gone + assert worker.wait(5000) + for _ in range(20): + app.processEvents() + time.sleep(0.01) + assert worker not in ioc_finder_qt._running_workers diff --git a/tests/test_live_values.py b/tests/test_live_values.py new file mode 100644 index 0000000..9edd831 --- /dev/null +++ b/tests/test_live_values.py @@ -0,0 +1,289 @@ +"""Tests for eco.widgets.live_values: the toolkit-independent reading/writing +behind the Qt, ipywidgets and Tk property grids.""" + +import threading +import time + +from eco.widgets import live_values + + +def wait_for(condition, seconds=3.0): + deadline = time.monotonic() + seconds + while time.monotonic() < deadline: + if condition(): + return True + time.sleep(0.01) + return condition() + + +class Item: + def __init__(self, name, value=1.0, delay=0.0): + self.name = name + self.value = value + self.delay = delay + self.reads = 0 + + def get_current_value(self): + self.reads += 1 + time.sleep(self.delay) + return self.value + + +class Monitored(Item): + def __init__(self, name): + super().__init__(name) + self.callbacks = {} + + def add_value_callback(self, callback, index=None): + index = len(self.callbacks) + 1 + self.callbacks[index] = callback + return index + + def clear_value_callback(self, index=None): + self.callbacks.pop(index, None) + + def push(self, value): + self.value = value + for callback in list(self.callbacks.values()): + callback(pvname=self.name, value=value) + + +class CallbackEpicsLike: + """What set_current_value_callback returns for PV-backed items.""" + + def __init__(self, func): + self.func = func + self.started = False + + def start(self, add_current_value=True, with_ctrlvars=True, auto_monitor=True): + self.started = (add_current_value, with_ctrlvars) + + def stop(self): + self.started = None + + +class PvMonitored(Item): + def set_current_value_callback(self, func="accumulate", **kwargs): + self.monitor = CallbackEpicsLike(func) + return self.monitor + + +def collect(): + results = [] + return results, lambda index, ok, payload: results.append((index, ok, payload)) + + +def test_monitored_items_update_at_once_and_unsubscribe_on_stop(): + results, on_result = collect() + item = Monitored("m") + reader = live_values.LiveReader(on_result, poll_interval=30.0) + reader.add(item) + reader.start() + try: + assert wait_for(lambda: item.callbacks) + assert wait_for(lambda: results) # the first read + assert not reader.monitored(0) # subscribed, but no event yet: not trusted + item.push(5.0) + assert wait_for(lambda: results[-1] == (0, True, 5.0), 1.0) # not after 30 s + assert reader.monitored(0) + finally: + reader.stop() + assert wait_for(lambda: not item.callbacks) + + +def test_a_monitor_without_parents_is_not_trusted(): + """e.g. DetectorVirtual([], fn) -- vacuum valve readbacks, the prepump state.""" + + class Composed: + children = [] + + def start(self, **kwargs): + pass + + def stop(self): + pass + + item = Item("readback") + item.set_current_value_callback = lambda func=None, **kw: Composed() + assert live_values.subscribe(item, lambda: None) is None + + +def test_a_monitor_that_misses_changes_is_demoted(monkeypatch): + """e.g. PshellMotor: calls back only when a move ends.""" + monkeypatch.setattr(live_values, "MONITORED_POLL_INTERVAL", 0.3) + results, on_result = collect() + item = Monitored("pshell") + reader = live_values.LiveReader(on_result, poll_interval=0.2) + reader.add(item) + reader.start() + try: + assert wait_for(lambda: item.callbacks) + item.push(1.0) # a move ended: an event + assert wait_for(lambda: reader.monitored(0)) + time.sleep(0.5) # the read that event asked for is done + item.value = 2.0 # the next move: no event + assert wait_for(lambda: not reader.monitored(0), 2.0) + assert wait_for(lambda: results[-1] == (0, True, 2.0)) + finally: + reader.stop() + + +def test_pv_style_monitor_is_started_without_blocking_reads(): + reader = live_values.LiveReader(lambda *a: None, poll_interval=30.0) + item = PvMonitored("pv") + reader.add(item) + reader.start() + try: + assert wait_for(lambda: getattr(item, "monitor", None) is not None and item.monitor.started) + assert item.monitor.started == (False, False) # no seeding get, no ctrlvars get + finally: + reader.stop() + assert wait_for(lambda: item.monitor.started is None) + + +def test_a_slow_item_does_not_hold_up_the_others(): + results, on_result = collect() + slow, fast = Item("slow", delay=2.0), Item("fast", value=3.0) + reader = live_values.LiveReader(on_result, poll_interval=0.1) + reader.add(slow) + reader.add(fast) + reader.start() + try: + assert wait_for(lambda: sum(1 for r in results if r[0] == 1) >= 3, 1.5) + finally: + reader.stop() + + +def test_no_value_and_errors_are_reported(): + results, on_result = collect() + + class Broken: + def get_current_value(self): + raise RuntimeError("IOC down") + + dead = Item("dead", value=None) + reader = live_values.LiveReader(on_result, poll_interval=30.0) + reader.add(dead) + reader.add(Broken()) + reader.start() + try: + assert wait_for(lambda: {r[0] for r in results} == {0, 1}) + finally: + reader.stop() + by_index = {r[0]: r for r in results} + assert by_index[0] == (0, False, "no value (not connected?)") + assert by_index[1] == (1, False, "RuntimeError: IOC down") + + +def test_inactive_items_are_not_read_until_activated_and_restart_works(): + results, on_result = collect() + item = Item("hidden") + reader = live_values.LiveReader(on_result, poll_interval=30.0) + reader.add(item, active=False) + reader.start() + time.sleep(0.3) + assert item.reads == 0 + reader.set_active(0, True) + assert wait_for(lambda: item.reads == 1) + reader.stop() + reader.start() # e.g. make_assembly_widget's start() after stop() + try: + assert wait_for(lambda: item.reads == 2) + finally: + reader.stop() + + +class Changer: + def __init__(self, seconds): + self.done = threading.Event() + self.seconds = seconds + self.stopped = False + + def wait(self): + self.done.wait(self.seconds) + + def stop(self): + self.stopped = True + self.done.set() + + +def test_start_write_reports_the_move_and_errors(): + states = [] + finished = threading.Event() + item = Item("mot") + holder = {} + item.set_target_value = lambda value: Changer(0.3) + live_values.start_write(item, lambda: 2.0, lambda *s: states.append(s), finished.set, holder) + assert wait_for(finished.is_set) + assert states == [("moving", "to 2.0"), ("idle", "")] + assert isinstance(holder["changer"], Changer) + + states.clear() + finished.clear() + + def blocked(value): + raise PermissionError("mot is in use by queue 'default', task #1 -- not moved.") + + item.set_target_value = blocked + live_values.start_write(item, lambda: 2.0, lambda *s: states.append(s), finished.set) + assert wait_for(finished.is_set) + assert states == [("error", "mot is in use by queue 'default', task #1 -- not moved.")] + + +def test_stop_move_stops_the_changer_or_the_item(): + changer = Changer(5) + live_values.stop_move(None, changer, lambda *s: None) + assert wait_for(lambda: changer.stopped) + + item = Item("mot") + item.stopped = False + item.stop = lambda: setattr(item, "stopped", True) + live_values.stop_move(item, None, lambda *s: None) + assert wait_for(lambda: item.stopped) + + +def test_prefetch_has_a_deadline(): + slow, fast = Item("slow", delay=1.5), Item("fast", value=4.0) + t0 = time.monotonic() + values = live_values.prefetch_values([slow, fast], deadline=0.3) + assert time.monotonic() - t0 < 0.8 + assert values[id(slow)] is live_values.PENDING + assert values[id(fast)] == 4.0 + + +def test_step_sources(): + item = Item("mot") + assert live_values.initial_step(item, 1) == 1 + assert live_values.initial_step(item, 1.0) == 0.1 + item._tweak_instance = type("T", (), {"step_sizes": [0.02]})() + assert live_values.initial_step(item, 1.0) == 0.02 + + class Pv: + def __init__(self): + self.puts = [] + + def get(self, timeout=None): + return 0.5 + + def put(self, value): + self.puts.append(value) + + class Motor(Item): + def _tweak_ioc(self): + pass + + motor = Motor("m") + pv = Pv() + motor._motor = type("M", (), {"get_pv": lambda _, field: pv})() + assert live_values.has_ioc_step(motor) and not live_values.has_ioc_step(item) + got = [] + holder = live_values.load_ioc_step(motor, got.append) + assert wait_for(lambda: got == [0.5]) + live_values.store_ioc_step(holder, 0.25) + assert wait_for(lambda: pv.puts == [0.25]) + + +def test_dot_state(): + assert live_values.dot_state("moving", "to 2", ("polled", ""))[1] == "moving: to 2" + assert live_values.dot_state("idle", "", ("live", ""))[1] == "connected, updated by monitor" + assert live_values.dot_state("error", "x", ("live", ""))[0] == "#d93025"