remove thread garbage collection
This commit is contained in:
@@ -44,13 +44,12 @@ def app_run(app_opts: ClanAppOptions) -> int:
|
||||
) -> SuccessDataClass[None] | ErrorDataClass:
|
||||
"""Cancel a task by its op_key."""
|
||||
log.info(f"Cancelling task with op_key: {task_id}")
|
||||
with webview.lock:
|
||||
if task_id in webview.threads:
|
||||
future = webview.threads[task_id]
|
||||
future.stop_event.set()
|
||||
log.info(f"Task {task_id} cancelled.")
|
||||
else:
|
||||
log.warning(f"Task {task_id} not found.")
|
||||
if task_id in webview.threads:
|
||||
future = webview.threads[task_id]
|
||||
future.stop_event.set()
|
||||
log.info(f"Task {task_id} cancelled.")
|
||||
else:
|
||||
log.warning(f"Task {task_id} not found.")
|
||||
return SuccessDataClass(
|
||||
op_key=op_key,
|
||||
data=None,
|
||||
@@ -63,8 +62,7 @@ def app_run(app_opts: ClanAppOptions) -> int:
|
||||
) -> SuccessDataClass[list[str]] | ErrorDataClass:
|
||||
"""List all tasks."""
|
||||
log.info("Listing all tasks.")
|
||||
with webview.lock:
|
||||
tasks = list(webview.threads.keys())
|
||||
tasks = list(webview.threads.keys())
|
||||
return SuccessDataClass(
|
||||
op_key=op_key,
|
||||
data=tasks,
|
||||
|
||||
@@ -2,7 +2,6 @@ import ctypes
|
||||
import json
|
||||
import logging
|
||||
import threading
|
||||
import time
|
||||
from collections.abc import Callable
|
||||
from dataclasses import dataclass
|
||||
from enum import IntEnum
|
||||
@@ -54,9 +53,6 @@ class Webview:
|
||||
self._handle = _webview_lib.webview_create(int(debug), window)
|
||||
self._callbacks: dict[str, Callable[..., Any]] = {}
|
||||
self.threads: dict[str, WebThread] = {}
|
||||
self.stopped_threads: set[str] = set()
|
||||
self.lock = threading.Lock()
|
||||
self.stop_garbage_collection: threading.Event = threading.Event()
|
||||
|
||||
if size:
|
||||
self.size = size
|
||||
@@ -94,28 +90,9 @@ class Webview:
|
||||
def navigate(self, url: str) -> None:
|
||||
_webview_lib.webview_navigate(self._handle, _encode_c_string(url))
|
||||
|
||||
def collect_garbage(self) -> None:
|
||||
while not self.stop_garbage_collection.is_set():
|
||||
with self.lock:
|
||||
for op_key in list(self.threads.keys()):
|
||||
if op_key in self.stopped_threads:
|
||||
log.debug(f"Collecting garbage op_key: {op_key}")
|
||||
del self.threads[op_key]
|
||||
self.stopped_threads.remove(op_key)
|
||||
if self.stop_garbage_collection.is_set():
|
||||
break
|
||||
time.sleep(1)
|
||||
|
||||
def run(self) -> None:
|
||||
thread = threading.Thread(
|
||||
target=self.collect_garbage, name="WebviewGarbageCollector"
|
||||
)
|
||||
thread.start()
|
||||
_webview_lib.webview_run(self._handle)
|
||||
self.stop_garbage_collection.set()
|
||||
log.info("Shutting down webview...")
|
||||
if self.lock.locked():
|
||||
self.lock.release()
|
||||
self.destroy()
|
||||
|
||||
def bind_jsonschema_api(self, api: MethodRegistry) -> None:
|
||||
@@ -178,17 +155,16 @@ class Webview:
|
||||
)
|
||||
self.return_(seq.decode(), FuncStatus.FAILURE, serialized)
|
||||
finally:
|
||||
self.stopped_threads.add(seq.decode())
|
||||
del self.threads[op_key]
|
||||
|
||||
stop_event = threading.Event()
|
||||
thread = threading.Thread(
|
||||
target=thread_task, args=(stop_event,), name="WebviewThread"
|
||||
)
|
||||
thread.start()
|
||||
with self.lock:
|
||||
self.threads[seq.decode()] = WebThread(
|
||||
thread=thread, stop_event=stop_event
|
||||
)
|
||||
self.threads[seq.decode()] = WebThread(
|
||||
thread=thread, stop_event=stop_event
|
||||
)
|
||||
|
||||
c_callback = _webview_lib.CFUNCTYPE(
|
||||
None, ctypes.c_char_p, ctypes.c_char_p, ctypes.c_void_p
|
||||
|
||||
Reference in New Issue
Block a user