mirror of
https://github.com/tiqi-group/pydase.git
synced 2025-04-20 08:20:02 +02:00
simplifies @task decorator (updates types), moves task logic into Task's run_task()
This commit is contained in:
parent
96cc7b31b4
commit
c0e5a77d6f
@ -1,5 +1,3 @@
|
||||
import asyncio
|
||||
import functools
|
||||
import logging
|
||||
from collections.abc import Callable, Coroutine
|
||||
from typing import Any, TypeVar
|
||||
@ -13,18 +11,17 @@ R = TypeVar("R")
|
||||
|
||||
def task(
|
||||
*, autostart: bool = False
|
||||
) -> Callable[[Callable[[Any], Coroutine[None, None, R]]], Task[R]]:
|
||||
) -> Callable[
|
||||
[
|
||||
Callable[[Any], Coroutine[None, None, R]]
|
||||
| Callable[[], Coroutine[None, None, R]]
|
||||
],
|
||||
Task[R],
|
||||
]:
|
||||
def decorator(
|
||||
func: Callable[[Any], Coroutine[None, None, R]],
|
||||
func: Callable[[Any], Coroutine[None, None, R]]
|
||||
| Callable[[], Coroutine[None, None, R]],
|
||||
) -> Task[R]:
|
||||
@functools.wraps(func)
|
||||
async def wrapper(self: Any) -> R | None:
|
||||
try:
|
||||
return await func(self)
|
||||
except asyncio.CancelledError:
|
||||
logger.info("Task '%s' was cancelled", func.__name__)
|
||||
return None
|
||||
|
||||
return Task(wrapper, autostart=autostart)
|
||||
return Task(func, autostart=autostart)
|
||||
|
||||
return decorator
|
||||
|
@ -96,7 +96,11 @@ class Task(pydase.data_service.data_service.DataService, Generic[R]):
|
||||
logger.info("Starting task %r", self._func_name)
|
||||
self._status = TaskStatus.RUNNING
|
||||
res: Coroutine[None, None, R] = self._bound_func()
|
||||
return await res
|
||||
try:
|
||||
return await res
|
||||
except asyncio.CancelledError:
|
||||
logger.info("Task '%s' was cancelled", self._func_name)
|
||||
return None
|
||||
logger.warning(
|
||||
"Cannot start task %r. Function has not been bound yet", self._func_name
|
||||
)
|
||||
|
Loading…
x
Reference in New Issue
Block a user