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

Added Collector Signal #227

Merged
merged 2 commits into from
Jul 15, 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: 28 additions & 0 deletions vessim/signal.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
from pathlib import Path
from typing import Any, Optional, Literal
from itertools import count
from threading import Event, Thread

import time
import pandas as pd
import numpy as np

Expand Down Expand Up @@ -404,3 +406,29 @@ def set_value(self, value: float) -> None:

def now(self, at: Optional[DatetimeLike] = None, **kwargs):
return self._v


class CollectorSignal(Signal, ABC):
def __init__(self, interval: int = 1):
super().__init__()
self.interval = interval
self._v = 0.0
self._stop_event = Event()
self._thread = Thread(target=self._collect_loop, daemon=True)
self._thread.start()

def now(self, at=None, **_) -> float:
return self._v

@abstractmethod
def collect(self) -> float:
pass

def _collect_loop(self) -> None:
while not self._stop_event.is_set():
self._v = self.collect()
time.sleep(self.interval)

def finalize(self) -> None:
self._stop_event.set()
self._thread.join()
Loading