Task Management Example¶
This guide provides a professional, step-by-step walkthrough for creating, registering, and executing a custom task using the TaskManager
and Task
classes from the agenticaiframework
package.
It is intended for developers who want to define reusable, modular units of work for their agents.
Prerequisites & Configuration¶
- Installation: Ensure
agenticaiframework
is installed and accessible in your Python environment. - No additional configuration is required for this example.
- Python Version: Compatible with Python 3.8+.
Code¶
from agenticaiframework.tasks import TaskManager, Task
if __name__ == "__main__":
task_manager = TaskManager()
# Define a custom task
class AdditionTask(Task):
def run(self, a, b):
result = a + b
print(f"Task Result: {result}")
return result
# Create and register the task
addition_task = AdditionTask(name="AdditionTask")
task_manager.register_task(addition_task)
# Execute the task
addition_task.run(5, 7)
# List registered tasks
print("Registered Tasks:", [task.name for task in task_manager.tasks])
# Retrieve a specific task
retrieved_task = task_manager.get_task("AdditionTask")
print("Retrieved Task:", retrieved_task.name)
Step-by-Step Execution¶
-
Import Required Classes
ImportTaskManager
andTask
fromagenticaiframework.tasks
. -
Instantiate the Task Manager
Create an instance ofTaskManager
to handle task registration and execution. -
Define a Custom Task
Create a class inheriting fromTask
and implement therun
method with the desired logic. -
Create the Task Instance
Instantiate your custom task with a unique name. -
Register the Task
Useregister_task
to add the task to the manager's registry. -
Execute the Task
Call therun
method with the required arguments. -
List Registered Tasks
Access thetasks
list to see all registered tasks. -
Retrieve a Specific Task
Useget_task
to fetch a task by name.
Best Practice: Keep tasks focused on a single responsibility to make them easier to test and reuse.
Expected Input¶
No user input is required; the script uses hardcoded values for demonstration purposes. In production, task parameters could be dynamically generated from user input, workflows, or other runtime data.
Expected Output¶
[YYYY-MM-DD HH:MM:SS] [Task:AdditionTask] Running task 'AdditionTask'
[YYYY-MM-DD HH:MM:SS] [Task:AdditionTask] Task 'AdditionTask' completed successfully
Task Result: 12
[YYYY-MM-DD HH:MM:SS] [TaskManager] Registered task 'AdditionTask' with ID <UUID>
Registered Tasks: ['AdditionTask']
Retrieved Task: AdditionTask
How to Run¶
Run the example from the project root:
python examples/tasks_example.py
If installed as a package, you can also run it from anywhere:
python -m examples.tasks_example
Tip: Combine
TaskManager
withAgentManager
to assign and execute tasks dynamically within agents.