Skip to content

Commit 69ac841

Browse files
committed
perf: pyinstrument integration
1 parent 8ca3bda commit 69ac841

File tree

5 files changed

+975
-836
lines changed

5 files changed

+975
-836
lines changed

agents-core/vision_agents/core/agents/agents.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -424,20 +424,22 @@ async def finish(self):
424424
Subscribes to the edge transport's `call_ended` event and awaits it. If
425425
no connection is active, returns immediately.
426426
"""
427-
# If connection is None or already closed, return immediately
428427
if not self._connection:
429428
logging.info("🔚 Agent connection already closed, finishing immediately")
430429
return
431430

431+
running_event = asyncio.Event()
432+
432433
@self.edge.events.subscribe
433434
async def on_ended(event: CallEndedEvent):
434-
self._is_running = False
435+
running_event.set()
435436

436-
while self._is_running:
437-
try:
438-
await asyncio.sleep(0.0001)
439-
except asyncio.CancelledError:
440-
self._is_running = False
437+
# TODO: add members count check (particiapnts left + count = 1 timeout 2 minutes)
438+
439+
try:
440+
await running_event.wait()
441+
except asyncio.CancelledError:
442+
running_event.clear()
441443

442444
await asyncio.shield(self.close())
443445

examples/01_simple_agent_example/pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name = "simple-agents-example"
33
version = "0.0.0"
44
requires-python = ">=3.13"
55

6-
# put only what this example needs
6+
# Dependencies that the example actually uses
77
dependencies = [
88
"python-dotenv>=1.0",
99
"vision-agents-plugins-deepgram",
@@ -19,6 +19,7 @@ dependencies = [
1919
"anthropic>=0.66.0",
2020
"google-genai>=1.33.0",
2121
"fal-client>=0.5.3",
22+
"pyinstrument>=5.1.1",
2223
]
2324

2425
[tool.uv.sources]
@@ -30,5 +31,4 @@ dependencies = [
3031
"vision-agents-plugins-smart-turn" = {path = "../../plugins/smart_turn", editable=true}
3132
"vision-agents-plugins-cartesia" = {path = "../../plugins/cartesia", editable=true}
3233
"vision-agents-plugins-gemini" = {path = "../../plugins/gemini", editable=true}
33-
3434
"vision-agents" = {path = "../../agents-core", editable=true}

examples/01_simple_agent_example/simple_agent_example.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import asyncio
22
from uuid import uuid4
33
from dotenv import load_dotenv
4+
import pyinstrument
45

56
from vision_agents.core import User, Agent
67
from vision_agents.plugins import cartesia, deepgram, getstream, gemini
@@ -9,6 +10,8 @@
910

1011

1112
async def start_agent() -> None:
13+
profiler = pyinstrument.Profiler()
14+
profiler.start()
1215
llm = gemini.LLM("gemini-2.0-flash")
1316
# create an agent to run with Stream's edge, openAI llm
1417
agent = Agent(
@@ -56,6 +59,10 @@ async def start_agent() -> None:
5659
# run till the call ends
5760
await agent.finish()
5861

62+
profiler.stop()
63+
with open('profiled.html', 'w') as f:
64+
f.write(profiler.output_html())
65+
5966

6067
if __name__ == "__main__":
6168
asyncio.run(start_agent())

0 commit comments

Comments
 (0)