-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathbasic_background.py
More file actions
94 lines (74 loc) Β· 2.67 KB
/
basic_background.py
File metadata and controls
94 lines (74 loc) Β· 2.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/env python3
"""
Basic Background Tasks Example for PraisonAI Agents.
This example demonstrates how to use the background task system with Agent:
1. Create an Agent with background task support
2. Run agent tasks asynchronously
3. Track task progress
4. Handle task completion
Usage:
python basic_background.py
"""
import asyncio
from praisonaiagents import Agent
from praisonaiagents.background import BackgroundRunner, BackgroundConfig, TaskStatus
async def main():
print("=" * 60)
print("Agent-Centric Background Tasks Demo")
print("=" * 60)
# Create a background runner with config (used standalone, not passed to Agent)
runner = BackgroundRunner(config=BackgroundConfig(max_concurrent_tasks=3))
# Create an Agent
agent = Agent(
name="AsyncAssistant",
instructions="You are a helpful research assistant.",
)
print("\n--- Agent with Background Support Created ---")
print(f"Agent: {agent.name}")
print(f"Background runner: {runner is not None}")
# Define some example tasks
async def research_task(topic: str) -> str:
"""Simulate a research task."""
print(f" π Researching '{topic}'...")
await asyncio.sleep(1.5)
return f"Research on '{topic}' completed with 5 key findings"
async def analysis_task(data: str) -> str:
"""Simulate an analysis task."""
print(f" π Analyzing data...")
await asyncio.sleep(1.0)
return f"Analysis complete: {data}"
# Submit tasks via background runner
print("\n--- Submitting Background Tasks ---")
task1 = await runner.submit(
research_task,
args=("AI trends 2025",),
name="research_ai"
)
print(f"β
Submitted research task: {task1.id[:8]}")
task2 = await runner.submit(
analysis_task,
args=("market data",),
name="analyze_market"
)
print(f"β
Submitted analysis task: {task2.id[:8]}")
# List all tasks
print("\n--- Task Status ---")
for task in runner.tasks:
print(f" [{task.id[:8]}] {task.status.value}")
# Wait for tasks to complete
print("\n--- Waiting for Tasks ---")
await task1.wait(timeout=10.0)
await task2.wait(timeout=10.0)
# Final results
print("\n--- Results ---")
for task in [task1, task2]:
status = "β
" if task.status == TaskStatus.COMPLETED else "β"
print(f" {status} {task.result}")
# Cleanup
runner.clear_completed()
print("\nβ
Cleared completed tasks")
print("\n" + "=" * 60)
print("Demo Complete!")
print("=" * 60)
if __name__ == "__main__":
asyncio.run(main())