MCPcopy Create free account
hub / github.com/AgentOps-AI/agentops / demonstrate_async_operations

Function demonstrate_async_operations

examples/agno/agno_async_operations.py:32–86  ·  view source on GitHub ↗

Demonstrate concurrent execution of multiple AI agent tasks. This function creates multiple async tasks that execute concurrently rather than sequentially. Each task makes an independent API call to the AI model, and asyncio.gather() waits for all tasks to complete before returning

()

Source from the content-addressed store, hash-verified

30
31
32async def demonstrate_async_operations():
33 """
34 Demonstrate concurrent execution of multiple AI agent tasks.
35
36 This function creates multiple async tasks that execute concurrently rather than sequentially.
37 Each task makes an independent API call to the AI model, and asyncio.gather()
38 waits for all tasks to complete before returning results.
39
40 Performance benefit: Instead of 3 sequential calls taking ~90 seconds total,
41 concurrent execution typically completes in ~30 seconds.
42 """
43 tracer = agentops.start_trace(trace_name="Agno Async Operations Example")
44
45 try:
46 # Initialize AI agent with specified model
47 agent = Agent(model=OpenAIChat(id="gpt-4o-mini"))
48
49 async def task1():
50 """Query AI about Python programming language."""
51 response = await agent.arun("Explain Python programming language in one paragraph")
52 return f"Python: {response.content}"
53
54 async def task2():
55 """Query AI about JavaScript programming language."""
56 response = await agent.arun("Explain JavaScript programming language in one paragraph")
57 return f"JavaScript: {response.content}"
58
59 async def task3():
60 """Query AI for comparison between programming languages."""
61 response = await agent.arun("Compare Python and JavaScript briefly")
62 return f"Comparison: {response.content}"
63
64 # Execute all tasks concurrently using asyncio.gather()
65 results = await asyncio.gather(task1(), task2(), task3())
66
67 for i, result in enumerate(results, 1):
68 print(f"\nTask {i} Result:")
69 print(result)
70 print("-" * 50)
71
72 agentops.end_trace(tracer, end_state="Success")
73
74 except Exception as e:
75 print(f"An error occurred: {e}")
76 agentops.end_trace(tracer, end_state="Error")
77
78 # Let's check programmatically that spans were recorded in AgentOps
79 print("\n" + "=" * 50)
80 print("Now let's verify that our LLM calls were tracked properly...")
81 try:
82 agentops.validate_trace_spans(trace_context=tracer)
83 print("\n✅ Success! All LLM spans were properly recorded in AgentOps.")
84 except agentops.ValidationError as e:
85 print(f"\n❌ Error validating spans: {e}")
86 raise
87
88
89if __name__ == "__main__":

Callers 1

Calls 6

AgentClass · 0.90
task1Function · 0.85
task2Function · 0.85
task3Function · 0.85
start_traceMethod · 0.80
end_traceMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…