mirror of
https://github.com/tiqi-group/pydase.git
synced 2025-04-21 00:40:01 +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
|
import logging
|
||||||
from collections.abc import Callable, Coroutine
|
from collections.abc import Callable, Coroutine
|
||||||
from typing import Any, TypeVar
|
from typing import Any, TypeVar
|
||||||
@ -13,18 +11,17 @@ R = TypeVar("R")
|
|||||||
|
|
||||||
def task(
|
def task(
|
||||||
*, autostart: bool = False
|
*, 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(
|
def decorator(
|
||||||
func: Callable[[Any], Coroutine[None, None, R]],
|
func: Callable[[Any], Coroutine[None, None, R]]
|
||||||
|
| Callable[[], Coroutine[None, None, R]],
|
||||||
) -> Task[R]:
|
) -> Task[R]:
|
||||||
@functools.wraps(func)
|
return Task(func, autostart=autostart)
|
||||||
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 decorator
|
return decorator
|
||||||
|
@ -96,7 +96,11 @@ class Task(pydase.data_service.data_service.DataService, Generic[R]):
|
|||||||
logger.info("Starting task %r", self._func_name)
|
logger.info("Starting task %r", self._func_name)
|
||||||
self._status = TaskStatus.RUNNING
|
self._status = TaskStatus.RUNNING
|
||||||
res: Coroutine[None, None, R] = self._bound_func()
|
res: Coroutine[None, None, R] = self._bound_func()
|
||||||
|
try:
|
||||||
return await res
|
return await res
|
||||||
|
except asyncio.CancelledError:
|
||||||
|
logger.info("Task '%s' was cancelled", self._func_name)
|
||||||
|
return None
|
||||||
logger.warning(
|
logger.warning(
|
||||||
"Cannot start task %r. Function has not been bound yet", self._func_name
|
"Cannot start task %r. Function has not been bound yet", self._func_name
|
||||||
)
|
)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user