Merge pull request 'UI: make tasks cancleable' (#3586) from hsjobeki/clan-core:qubasas into main

Reviewed-on: https://git.clan.lol/clan/clan-core/pulls/3586
This commit is contained in:
hsjobeki
2025-05-12 16:24:45 +00:00
17 changed files with 857 additions and 260 deletions

View File

@@ -71,7 +71,9 @@ class AsyncContext:
prefix: str | None = None # prefix for logging
stdout: IO[bytes] | None = None # stdout of subprocesses
stderr: IO[bytes] | None = None # stderr of subprocesses
cancel: bool = False # Used to signal cancellation of task
should_cancel: Callable[[], bool] = (
lambda: False
) # Used to signal cancellation of task
@dataclass
@@ -92,7 +94,14 @@ def is_async_cancelled() -> bool:
"""
Check if the current task has been cancelled.
"""
return get_async_ctx().cancel
return get_async_ctx().should_cancel()
def set_should_cancel(should_cancel: Callable[[], bool]) -> None:
"""
Set the cancellation function for the current task.
"""
get_async_ctx().should_cancel = should_cancel
def get_async_ctx() -> AsyncContext:
@@ -313,7 +322,7 @@ class AsyncRuntime:
"""
for name, task in self.tasks.items():
if not task.finished:
task.async_opts.async_ctx.cancel = True
set_should_cancel(lambda: True)
log.debug(f"Canceling task {name}")

View File

@@ -32,12 +32,28 @@ class FileRequest:
initial_folder: str | None = field(default=None)
@API.register_abstract
def cancel_task(task_id: str) -> None:
"""Cancel a task by its op_key."""
msg = "cancel_task() is not implemented"
raise NotImplementedError(msg)
@API.register_abstract
def list_tasks() -> list[str]:
"""List all tasks."""
msg = "list_tasks() is not implemented"
raise NotImplementedError(msg)
@API.register_abstract
def open_file(file_request: FileRequest) -> list[str] | None:
"""
Abstract api method to open a file dialog window.
It must return the name of the selected file or None if no file was selected.
"""
msg = "open_file() is not implemented"
raise NotImplementedError(msg)
@dataclass