Skip to content

Commit

Permalink
minor updates
Browse files Browse the repository at this point in the history
  • Loading branch information
hanjinliu committed Aug 25, 2023
1 parent 6094b49 commit 3a9127e
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 20 deletions.
2 changes: 0 additions & 2 deletions magicclass/_gui/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1359,8 +1359,6 @@ def convert_attributes(
return _dict


# def _find_callback_level()

_dummy_macro = DummyMacro()


Expand Down
2 changes: 1 addition & 1 deletion magicclass/_gui/_macro_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ def _func(self, *args, **kwargs):

@functools_wraps(_func)
def _silent(bgui: MagicTemplate, *args, **kwargs):
with bgui._search_parent_magicclass().macro.blocked():
with bgui.macro.blocked():
out = _func.__get__(bgui)(*args, **kwargs)
return out

Expand Down
7 changes: 0 additions & 7 deletions magicclass/ext/dask/progress.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,6 @@ def _finish(self, dsk=None, state=None, errored=None):
self._thread_timer.join()
return None

def _on_timer_updated(self, _=None):
if self._timer.sec < 3600:
self.time_label.value = self._timer.format_time("{min:0>2}:{sec:0>2}")
else:
self.time_label.value = self._timer.format_time()
return None

def set_worker(self, worker: GeneratorWorker | FunctionWorker):
"""Set currently running worker."""
self._worker = worker
Expand Down
2 changes: 2 additions & 0 deletions magicclass/fields/_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,8 @@ def connect_async(self, func=None, *, timeout=0.0, ignore_errors=False):
from magicclass.utils import thread_worker

def _wrapper(fn):
if isinstance(fn, thread_worker):
return self.connect(fn)
_running = None
_last_run = 0.0

Expand Down
23 changes: 13 additions & 10 deletions magicclass/utils/qthreading/thread_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ def __init__(
self._ignore_errors = ignore_errors
self._objects: dict[int, BaseGui] = {}
self._progressbars: dict[int, ProgressBarLike | None] = {}
# recordable -> _recorder is a recording function
# not recordable, recursive=True -> _recorder is _silent
# not recordable, recursive=False -> _recorder is None
self._recorder: Callable[_P, Any] | None = None
self._validators: dict[str, Callable] | None = None
self._signature_cache = None
Expand Down Expand Up @@ -216,13 +219,10 @@ def callback(callback: Callable[_P, _R] = lambda: None) -> Callback[_P, _R]:
raise TypeError(f"{callback} is not callable.")
return Callback(callback)

@staticmethod
def run(meth: AsyncMethod[_P, _R]) -> Callable[_P, _R]:
return meth.arun

@classmethod
@contextmanager
def blocking_mode(cls):
"""Always run thread workers in the blocking mode."""
cls._BLOCKING_SOURCES.append(None)
try:
yield
Expand Down Expand Up @@ -306,9 +306,9 @@ def __get__(self, gui, objtype=None):
self._objects[gui_id] = _create_worker # cache
return _create_worker

def button(self, gui: BaseGui) -> Clickable:
"""The corresponding button object."""
return gui[self._func.__name__]
def _is_running(self, gui: BaseGui) -> bool:
btn = gui[self._func.__name__]
return getattr(btn, "running", False)

def _validate_args(self, gui: BaseGui, args, kwargs) -> tuple[tuple, dict]:
if self._validators is None:
Expand Down Expand Up @@ -356,13 +356,16 @@ def _run(*args, **kwargs):

return worker

def _is_non_blocking(self, gui: BaseGui) -> bool:
async_ok = self._force_async or self._is_running(gui)
not_blocking_mode = len(self._BLOCKING_SOURCES) == 0
return async_ok and not_blocking_mode

def _create_method(self, gui: BaseGui) -> Callable[_P, None]:
@_async_method
@wraps(self)
def _create_worker(*args, **kwargs):
_is_non_blocking = (self._force_async or self.button(gui).running) and len(
self._BLOCKING_SOURCES
) == 0
_is_non_blocking = self._is_non_blocking(gui)
with gui.macro.blocked():
args, kwargs = self._validate_args(gui, args, kwargs)

Expand Down

0 comments on commit 3a9127e

Please sign in to comment.