Skip to content

Commit

Permalink
Allow to use OverheadHelper without context manager
Browse files Browse the repository at this point in the history
  • Loading branch information
albireox committed Dec 6, 2023
1 parent 274165a commit 14ff740
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions src/hal/helpers/overhead.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,30 +39,41 @@ def __post_init__(self) -> None:

self.success: bool = False

async def __aenter__(self):
async def start(self):
"""Starts the timer."""

self.elapsed = 0
self.start_time = time.time()

async def __aenter__(self):
"""Starts the timer."""

await self.start()
return self

async def __aexit__(self, exc_type, exc_value, traceback):
"""Stops the timer and records the overhead."""
async def stop(self):
"""Stops the timer and records overheads."""

if self.start_time is None:
raise ValueError("Timer not started")

self.end_time = time.time()
self.elapsed = round(time.time() - self.start_time, 2)

await self.emit_keywords()
await asyncio.get_running_loop().run_in_executor(None, self.update_database)

return

async def __aexit__(self, exc_type, exc_value, traceback):
"""Stops the timer and records the overhead."""

if exc_type is not None:
self.success = False
else:
self.success = True

await self.emit_keywords()
asyncio.get_running_loop().call_soon(self.update_database)
await self.stop()

# __aexit__ will re-raise the exception if the return value is None/False.
return self.success
Expand Down

0 comments on commit 14ff740

Please sign in to comment.