Taskable Mixin
busylight_core.mixins.taskable
¶
Asynchronous task support for animating lights.
Attributes¶
busylight_core.mixins.taskable.TaskFunction
module-attribute
¶
busylight_core.mixins.taskable.TaskInstantiator
module-attribute
¶
TaskInstantiator = Callable[
[str, TaskFunction, float | None], Task | Timer
]
Classes¶
busylight_core.mixins.taskable.TaskPriority
¶
busylight_core.mixins.taskable.TaskInfo
dataclass
¶
Information about a managed task.
Contains task metadata including priority, creation time, and status information for enhanced task monitoring and debugging.
Attributes¶
busylight_core.mixins.taskable.TaskInfo.is_running
property
¶
Check if task is currently running.
Returns True if the task has not completed, been cancelled, or failed.
busylight_core.mixins.taskable.TaskInfo.is_cancelled
property
¶
Check if task was cancelled.
Returns True if the task was explicitly cancelled before completion.
busylight_core.mixins.taskable.TaskInfo.has_exception
property
¶
Check if task completed with an exception.
Returns True if the task completed but raised an unhandled exception.
busylight_core.mixins.taskable.TaskInfo.exception
property
¶
Get task exception if any.
Returns the exception that caused the task to fail, or None if the task completed successfully or is still running.
busylight_core.mixins.taskable.TaskableMixin
¶
Associate and manage asynchronous and synchronous tasks.
Provides enhanced task management with automatic strategy selection (asyncio vs threading), prioritization, error handling, and task monitoring capabilities for Light instances.
The environment automatically determines whether to use asyncio tasks or threading timers based on the presence of a running event loop.
Initialize TaskableMixin with task storage for both strategies.
Source code in src/busylight_core/mixins/taskable.py
Attributes¶
busylight_core.mixins.taskable.TaskableMixin.event_loop
cached
property
¶
The default event loop.
Returns the currently running event loop, or creates a new one if no event loop is currently running.
busylight_core.mixins.taskable.TaskableMixin.tasks
cached
property
¶
Active asyncio tasks that are associated with this instance.
Dictionary mapping task names to their corresponding asyncio.Task objects.
busylight_core.mixins.taskable.TaskableMixin.task_info
cached
property
¶
Enhanced task information with priority and status tracking.
Dictionary mapping task names to TaskInfo objects containing metadata about priority, creation time, and current status.
busylight_core.mixins.taskable.TaskableMixin.task_strategy
cached
property
¶
Return the appropriate task instantiation function based on environment.
Automatically detects if we're in an asyncio context and returns the corresponding task creation function. Both functions have identical call signatures for transparent usage.
:return: Function to create tasks (asyncio or threading based)
Functions¶
busylight_core.mixins.taskable.TaskableMixin.add_task
¶
add_task(
name,
func,
priority=NORMAL,
replace=False,
interval=None,
)
Create a task using environment-appropriate strategy.
The environment (asyncio vs non-asyncio) automatically determines whether to use asyncio tasks or threading timers. Both strategies support the same function signatures and periodic execution.
:param name: Unique identifier for the task :param func: Function to execute (sync or async) :param priority: Task priority (used for asyncio tasks only) :param replace: Whether to replace existing task with same name :param interval: For periodic tasks, repeat interval in seconds :return: Created asyncio.Task or threading.Timer
Source code in src/busylight_core/mixins/taskable.py
busylight_core.mixins.taskable.TaskableMixin.cancel_task
¶
Cancel task regardless of which strategy created it.
:param name: Name of task to cancel :return: The cancelled task/timer or None if not found
Source code in src/busylight_core/mixins/taskable.py
busylight_core.mixins.taskable.TaskableMixin.cancel_tasks
¶
Cancel all tasks or tasks of specific priority.
Cancels either all tasks or only tasks matching the specified priority level.
:param priority: If specified, only cancel tasks of this priority level
Source code in src/busylight_core/mixins/taskable.py
busylight_core.mixins.taskable.TaskableMixin.get_task_status
¶
Get detailed status information for a task.
Returns comprehensive status information including running state, exceptions, priority, and creation time.
:param name: Name of task to check :return: Dictionary with task status details or None if not found
Source code in src/busylight_core/mixins/taskable.py
busylight_core.mixins.taskable.TaskableMixin.list_active_tasks
¶
Get list of currently active task names.
Returns sorted list of task names that are currently running.
:return: List of task names that are currently running