Skip to content
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

Probe modul ready signal #122

Merged
merged 3 commits into from
Mar 12, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 24 additions & 4 deletions everest-testing/src/everest/testing/core_utils/probe_module.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
import asyncio
import logging
import datetime
import threading

from queue import Queue
from typing import Any, Callable

from everest.framework import Module, RuntimeSession

def display_msg(logger, msg: str):
logger(" %s THREAD: %s -> %s", datetime.datetime.now(), threading.get_ident(), msg)

def debug_msg(msg: str):
display_msg(logging.debug, msg)

def info_msg(msg: str):
display_msg(logging.info, msg)

class ProbeModule:
"""
Expand All @@ -24,7 +35,7 @@ def __init__(self, session: RuntimeSession, module_id="probe"):
m = Module(module_id, session)
self._setup = m.say_hello()
self._mod = m
self._ready_event = asyncio.Event()
self._ready_event = threading.Event()
self._started = False

def start(self):
Expand Down Expand Up @@ -106,14 +117,23 @@ def _ready(self):
Internal function: callback triggered by the EVerest framework when all modules have been initialized
This is equivalent to the ready() method in C++ modules
"""
logging.info("ProbeModule ready")
self._ready_event.set()

async def wait_to_be_ready(self, timeout=3):
async def wait_for_event(self, timeout: float):
"""
Helper to make threading.Event behave similar to asyncio.Event, which is awaitable and raising TimeoutError.
- timeout: Time to for ready_event
"""
self._ready_event.wait(timeout)
if not self._ready_event.is_set():
raise TimeoutError("Waiting for ready: timeout")


async def wait_to_be_ready(self, timeout=3.0):
"""
Convenience method which allows you to wait until the _ready() callback is triggered (i.e. until EVerest is up and running)
"""
if not self._started:
raise RuntimeError("Called wait_to_be_ready(), but probe module has not been started yet! "
"Please use start() to start the module first.")
await asyncio.wait_for(asyncio.to_thread(self._ready_event.wait), timeout)
await self.wait_for_event(timeout)
Loading