8000 Correct threading usage in vendored AWB code by edan-bainglass · Pull Request #249 · aiidalab/aiidalab-home · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8000
23 changes: 17 additions & 6 deletions home/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import re
import sys
import threading
import time
import traceback
import uuid
import warnings
Expand Down Expand Up @@ -431,7 +430,7 @@ def _observe_process(self, change):
with self._monitor_thread_lock:
self._monitor_thread_stop.clear()
self._monitor_thread = threading.Thread(
target=self._monitor_process, args=(process_uuid,)
target=self._monitor_process, args=(process_uuid,), daemon=True
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Making the thread daemonic so that it does not hold up a kernel marked for termination

)
self._monitor_thread.start()

Expand Down Expand Up @@ -633,6 +632,8 @@ class ProcessListWidget(ipw.VBox):

def __init__(self, path_to_root="../", **kwargs):
self.path_to_root = path_to_root
self._autoupdate_thread = None
self._autoupdate_stop = threading.Event()
self.table = ipw.HTML()
self.output = ipw.HTML()
update_button = ipw.Button(description="Update now")
Expand Down Expand Up @@ -728,13 +729,23 @@ def _validate_process_label(self, provided):
return None

def _follow(self, update_interval):
while True:
while not self._autoupdate_stop.wait(update_interval):
self.update()
time.sleep(update_interval)

def start_autoupdate(self, update_interval=10):
update_state = threading.Thread(target=self._follow, args=(update_interval,))
update_state.start()
if self._autoupdate_thread is not None and self._autoupdate_thread.is_alive():
return
self._autoupdate_stop.clear()
self._autoupdate_thread = threading.Thread(
target=self._follow, args=(update_interval,), daemon=True
)
self._autoupdate_thread.start()

def stop_autoupdate(self):
self._autoupdate_stop.set()
if self._autoupdate_thread is not None:
self._autoupdate_thread.join()
self._autoupdate_thread = None


class RunningCalcJobOutputWidget(ipw.VBox):
Expand Down
41 changes: 41 additions & 0 deletions tests/test_process_threads.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from threading import Event
from types import SimpleNamespace

from home import process as home_process


def test_process_monitor_thread_is_daemon(monkeypatch):
monkeypatch.setattr(
home_process.orm, "load_node", lambda _: SimpleNamespace(is_sealed=False)
)
monitor = home_process.ProcessMonitor(timeout=0.01)
monitor.value = "process-uuid"
thread = monitor._monitor_thread
assert thread is not None
try:
assert thread.is_alive()
assert thread.daemon
finally:
monitor.value = None
assert not thread.is_alive()


def test_process_list_autoupdate_thread_stops(monkeypatch):
refreshed = Event()
monkeypatch.setattr(
home_process.ProcessListWidget, "update", lambda _: refreshed.set()
)
widget = home_process.ProcessListWidget()
refreshed.clear()
widget.start_autoupdate(update_interval=0.01)
thread = widget._autoupdate_thread
assert thread is not None
try:
assert refreshed.wait(timeout=2)
assert thread.is_alive()
assert thread.daemon
widget.start_autoupdate(update_interval=0.01)
assert widget._autoupdate_thread is thread
finally:
widget.stop_autoupdate()
assert not thread.is_alive()
Loading
0