task_manager: return task directly instead of uuid

This commit is contained in:
Jörg Thalheim
2023-10-03 17:48:56 +02:00
parent 60309f94dc
commit f8e5f261f9
3 changed files with 15 additions and 15 deletions

View File

@@ -5,7 +5,7 @@ import select
import shlex
import subprocess
import threading
from typing import Any, Iterator
from typing import Any, Iterator, Type, TypeVar
from uuid import UUID, uuid4
@@ -156,14 +156,15 @@ def get_task(uuid: UUID) -> BaseTask:
return POOL[uuid]
def register_task(task: type, *args: Any) -> UUID:
T = TypeVar("T", bound="BaseTask")
def create_task(task_type: Type[T], *args: Any) -> T:
global POOL
if not issubclass(task, BaseTask):
raise TypeError("task must be a subclass of BaseTask")
uuid = uuid4()
inst_task = task(uuid, *args)
POOL[uuid] = inst_task
inst_task.start()
return uuid
task = task_type(uuid, *args)
POOL[uuid] = task
task.start()
return task