Action decomposition and execution framework
Project description
Action Trees
Documentation: https://roxautomation.gitlab.io/action-trees
Source Code: https://gitlab.com/roxautomation/action-trees
Summary
Action Trees is an asyncio
-based Python library for managing hierarchical and asynchronous tasks. It breaks down complex tasks into simpler, independent steps that can be run sequentially or in parallel.
Functionality
ActionItem
Class: Central class for representing tasks, supporting states likeINITIALIZING
,RUNNING
,PAUSED
, andFAILED
.- Task Management: Provides APIs to start, pause, resume, and cancel tasks asynchronously.
- Hierarchical Actions: Actions can have child actions, supporting structured, tree-like task management.
- Parallel Execution: Supports running child tasks concurrently using
asyncio
. - State and Exception Handling: Handles task state transitions and exceptions, ensuring proper state flow.
Example
The following example illustrates a high-level task to prepare and make a cappuccino using the ActionItem
class:
import asyncio
from action_trees import ActionItem
class AtomicAction(ActionItem):
"""Basic machine action with no children."""
def __init__(self, name: str, duration: float = 0.1):
super().__init__(name=name)
self._duration = duration
async def _on_run(self) -> None:
await asyncio.sleep(self._duration)
class PrepareMachineAction(ActionItem):
"""Prepare the machine."""
def __init__(self) -> None:
super().__init__(name="prepare")
self.add_child(AtomicAction(name="initialize"))
self.add_child(AtomicAction(name="clean"))
async def _on_run(self) -> None:
for child in self.children:
await child.start()
class MakeCappuccinoAction(ActionItem):
"""Make cappuccino."""
def __init__(self) -> None:
super().__init__(name="make_cappuccino")
self.add_child(AtomicAction(name="boil_water"))
self.add_child(AtomicAction(name="grind_coffee"))
async def _on_run(self) -> None:
await self.run_children_parallel()
class CappuccinoOrder(ActionItem):
"""High-level action to make a cappuccino."""
def __init__(self) -> None:
super().__init__(name="cappuccino_order")
self.add_child(PrepareMachineAction())
self.add_child(MakeCappuccinoAction())
async def _on_run(self) -> None:
for child in self.children:
await child.start()
async def main() -> None:
order = CappuccinoOrder()
await order.start()
order.display_tree()
if __name__ == "__main__":
asyncio.run(main())
This code creates a hierarchical task to prepare a machine and make a cappuccino, with some actions running sequentially and others in parallel.
More examples : see examples
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file action_trees-1.0.0.tar.gz
.
File metadata
- Download URL: action_trees-1.0.0.tar.gz
- Upload date:
- Size: 10.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.0 CPython/3.12.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 565ad29eb741b7fdcd8335f4b2ccfe81b34dc4916ccefcc67e4179e6a459e22d |
|
MD5 | 6d2a0741a9fd63e0befceddd32f6ba3f |
|
BLAKE2b-256 | b6d4ff8333007c1c6349c346436ea36fcb031eec9ff1ff38a5be2520d093f07b |
File details
Details for the file action_trees-1.0.0-py3-none-any.whl
.
File metadata
- Download URL: action_trees-1.0.0-py3-none-any.whl
- Upload date:
- Size: 8.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.0 CPython/3.12.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0b48fae7d75875fb423e850d85917388d39b156da377b50ae9c0e346f206f325 |
|
MD5 | 437b72062f295389d565af651e390a42 |
|
BLAKE2b-256 | b63630d2ec5716159726ad7218559534c4eecd1a97a1238ac97a5110f36bf9b5 |