- 
                Notifications
    
You must be signed in to change notification settings  - Fork 71
 
Optimize delays - realtime, waiting logic and error handling #132
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
264c3d0
              56dd65e
              340cfbc
              b559017
              29b7a8a
              6f0434e
              b70f17d
              8177d5d
              4c5cfcf
              fabf7b6
              16bc202
              File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| 
          
            
          
           | 
    @@ -85,3 +85,6 @@ stream-py/ | |
| *.pt | ||
| *.kef | ||
| *.onnx | ||
| profile.html | ||
| 
     | 
||
| /opencode.json | ||
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| from .base import Profiler | ||
| 
     | 
||
| __all__ = ["Profiler"] | 
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| import pyinstrument | ||
| import logging | ||
| 
     | 
||
| from vision_agents.core.events import EventManager | ||
| from vision_agents.core.agents import events | ||
| 
     | 
||
| logger = logging.getLogger(__name__) | ||
| 
     | 
||
| 
     | 
||
| class Profiler: | ||
| def __init__(self, output_path='./profile.html'): | ||
| self.output_path = output_path | ||
| self.events = EventManager() | ||
| self.events.register_events_from_module(events) | ||
| self.profiler = pyinstrument.Profiler() | ||
| self.profiler.start() | ||
| self.events.subscribe(self.on_finish) | ||
| 
     | 
||
| async def on_finish(self, event: events.AgentFinishEvent): | ||
| self.profiler.stop() | ||
| logger.info(f"Profiler stopped. Time file saved at: {self.output_path}") | ||
| with open(self.output_path, 'w') as f: | ||
| f.write(self.profiler.output_html()) | ||
| 
         
      Comment on lines
    
      +19
     to 
      +23
    
   
  There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion | 🟠 Major Add error handling and improve async file I/O. The  
 Apply this diff:  async def on_finish(self, event: events.AgentFinishEvent):
+    """Stop profiling and write HTML report when agent finishes.
+    
+    Args:
+        event: The AgentFinishEvent triggering profiler shutdown.
+    """
+    try:
-        self.profiler.stop()
-        logger.info(f"Profiler stopped. Time file saved at: {self.output_path}")
-        with open(self.output_path, 'w') as f:
-            f.write(self.profiler.output_html())
+        self.profiler.stop()
+        html_output = self.profiler.output_html()
+        
+        # Consider using aiofiles for async I/O
+        with open(self.output_path, 'w', encoding='utf-8') as f:
+            f.write(html_output)
+        
+        logger.info(f"Profiler stopped. Profile saved at: {self.output_path}")
+    except Exception as e:
+        logger.error(f"Failed to write profiler output to {self.output_path}: {e}", exc_info=True)Or, for fully async I/O: import aiofiles
async def on_finish(self, event: events.AgentFinishEvent):
    """Stop profiling and write HTML report when agent finishes.
    
    Args:
        event: The AgentFinishEvent triggering profiler shutdown.
    """
    try:
        self.profiler.stop()
        html_output = self.profiler.output_html()
        
        async with aiofiles.open(self.output_path, 'w', encoding='utf-8') as f:
            await f.write(html_output)
        
        logger.info(f"Profiler stopped. Profile saved at: {self.output_path}")
    except Exception as e:
        logger.error(f"Failed to write profiler output to {self.output_path}: {e}", exc_info=True)🤖 Prompt for AI Agents | 
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion | 🟠 Major
Add docstrings and improve profiler lifecycle management.
Multiple concerns with the
Profilerclass:Missing docstrings: The class and
__init__method lack docstrings. As per coding guidelines, docstrings must follow the Google style guide.Premature profiling start: The profiler starts immediately on line 16, even before the agent joins a call. This captures initialization overhead that may not be relevant to profiling agent behavior during a call. Consider starting the profiler when
AgentInitEventis received instead.No cleanup mechanism: If
AgentFinishEventis never emitted (e.g., iffinish()is never called or the process crashes), the profiler runs indefinitely and the HTML file is never written. Consider:__enter__/__exit__)stop()method for manual cleanup__del__) as a fallbackNo output path validation: Line 11 accepts any path without validation. If the directory doesn't exist or isn't writable, the error won't surface until
on_finish.Here's a proposed refactor:
🤖 Prompt for AI Agents