Skip to content

Commit

Permalink
Convert a few things to primitives
Browse files Browse the repository at this point in the history
  • Loading branch information
vkottler committed Sep 8, 2023
1 parent 3dea6db commit 43cec49
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
8 changes: 6 additions & 2 deletions runtimepy/net/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from runtimepy.channel.environment import ChannelEnvironment
from runtimepy.metrics import ConnectionMetrics
from runtimepy.mixins.environment import ChannelEnvironmentMixin
from runtimepy.primitives import Bool
from runtimepy.primitives.byte_order import DEFAULT_BYTE_ORDER, ByteOrder

BinaryMessage = _Union[bytes, bytearray, memoryview]
Expand All @@ -41,7 +42,7 @@ def __init__(
"""Initialize this connection."""

_LoggerMixin.__init__(self, logger=logger)
self._enabled = True
self._enabled = Bool(True)

# A queue for out-going text messages. Connections that don't use
# this can set 'uses_text_tx_queue' to False to avoid scheduling a
Expand All @@ -65,6 +66,9 @@ def __init__(
if add_metrics:
self.register_connection_metrics(self.metrics)

# State.
self.env.channel("enabled", self._enabled)

self.init()

def init(self) -> None:
Expand Down Expand Up @@ -130,7 +134,7 @@ def disable(self, reason: str) -> None:
if self._enabled:
self.logger.info("Disabling connection: '%s'.", reason)
self.disable_extra()
self._enabled = False
self._enabled.value = False

# Cancel tasks.
for task in self._tasks:
Expand Down
29 changes: 21 additions & 8 deletions runtimepy/task/basic/periodic.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from runtimepy.metrics import PeriodicTaskMetrics
from runtimepy.mixins.environment import ChannelEnvironmentMixin
from runtimepy.primitives import Bool as _Bool
from runtimepy.primitives import Float as _Float


class PeriodicTask(_LoggerMixin, ChannelEnvironmentMixin, _ABC):
Expand All @@ -34,7 +35,7 @@ def __init__(
name: str,
average_depth: int = _DEFAULT_DEPTH,
metrics: PeriodicTaskMetrics = None,
period_s: float = None,
period_s: float = 1.0,
env: ChannelEnvironment = None,
) -> None:
"""Initialize this task."""
Expand All @@ -43,7 +44,7 @@ def __init__(
_LoggerMixin.__init__(self, logger=_getLogger(self.name))
self._task: _Optional[_asyncio.Task[None]] = None

self._period_s: _Optional[float] = None
self.period_s = _Float()
self.set_period(period_s=period_s)

# Setup runtime state.
Expand All @@ -56,17 +57,27 @@ def __init__(
ChannelEnvironmentMixin.__init__(self, env=env)
self.register_task_metrics(self.metrics)

# State.
self.env.channel("enabled", self._enabled)
self.env.channel("period", self.period_s)
self._init_state()

self._dispatch_rate = _RateTracker(depth=average_depth)
self._dispatch_time = _MovingAverage(depth=average_depth)

def _init_state(self) -> None:
"""Add channels to this instance's channel environment."""

def set_period(self, period_s: float = None) -> bool:
"""Attempt to set a new period for this task."""

result = False

if period_s is not None and self._period_s != period_s:
self._period_s = period_s
self.logger.info("Task rate set to %s.", _rate_str(period_s))
if period_s is not None and self.period_s != period_s:
self.period_s.value = period_s
self.logger.info(
"Task rate set to %s.", _rate_str(self.period_s.value)
)
result = True

return result
Expand Down Expand Up @@ -95,8 +106,10 @@ async def run(
self._enabled.raw.value = True

self.set_period(period_s=period_s)
assert self._period_s is not None, "Task period isn't set!"
self.logger.info("Task starting at %s.", _rate_str(self._period_s))
assert self.period_s is not None, "Task period isn't set!"
self.logger.info(
"Task starting at %s.", _rate_str(self.period_s.value)
)

eloop = _asyncio.get_running_loop()

Expand All @@ -122,7 +135,7 @@ async def run(
if stop_sig is not None:
self._enabled.raw.value = not stop_sig.is_set()

sleep_s = self._period_s - iter_time
sleep_s = self.period_s.value - iter_time

if self._enabled:
try:
Expand Down

0 comments on commit 43cec49

Please sign in to comment.