moves autostart from Task to separate autostart submodule

This commit is contained in:
Mose Müller 2024-09-13 12:37:18 +02:00
parent 416b9ee815
commit bfa0acedab
2 changed files with 21 additions and 3 deletions

View File

@ -0,0 +1,15 @@
import pydase.data_service.data_service
import pydase.task.task
from pydase.utils.helpers import is_property_attribute
def autostart_service_tasks(
service: pydase.data_service.data_service.DataService,
) -> None:
for attr in dir(service):
if not is_property_attribute(service, attr): # prevent eval of property attrs
val = getattr(service, attr)
if isinstance(val, pydase.task.task.Task) and val.autostart:
val.start()
elif isinstance(val, pydase.DataService):
autostart_service_tasks(val)

View File

@ -57,6 +57,12 @@ class Task(pydase.data_service.data_service.DataService, Generic[R]):
self._status = TaskStatus.NOT_RUNNING
self._result: R | None = None
@property
def autostart(self) -> bool:
"""Defines if the task should be started automatically when the `pydase.Server`
starts."""
return self._autostart
@property
def status(self) -> TaskStatus:
return self._status
@ -129,7 +135,4 @@ class Task(pydase.data_service.data_service.DataService, Generic[R]):
self._loop = asyncio.get_event_loop()
self._bound_func = self._func.__get__(instance, owner)
self._set_up = True
if self._autostart:
self.start()
return self