|
| 1 | +# License: MIT |
| 2 | +# Copyright © 2023 Frequenz Energy-as-a-Service GmbH |
| 3 | + |
| 4 | +"""A receiver that can be made ready through an event.""" |
| 5 | + |
| 6 | + |
| 7 | +import asyncio as _asyncio |
| 8 | + |
| 9 | +from frequenz.channels import _base_classes, _exceptions |
| 10 | + |
| 11 | + |
| 12 | +class Event(_base_classes.Receiver[None]): |
| 13 | + """A receiver that can be made ready through an event. |
| 14 | +
|
| 15 | + The receiver (the [`ready()`][frequenz.channels.util.Event.ready] method) will wait |
| 16 | + until [`set()`][frequenz.channels.util.Event.set] is called. At that point the |
| 17 | + receiver will wait again after the event is |
| 18 | + [`consume()`][frequenz.channels.Receiver.consume]d. |
| 19 | +
|
| 20 | + The receiver can be completely stopped by calling |
| 21 | + [`stop()`][frequenz.channels.Receiver.stop]. |
| 22 | +
|
| 23 | + Example: |
| 24 | + ```python |
| 25 | + import asyncio |
| 26 | + from frequenz.channels import Receiver |
| 27 | + from frequenz.channels.util import Event, select, selected_from |
| 28 | +
|
| 29 | + other_receiver: Receiver[int] = ... |
| 30 | + exit_event = Event() |
| 31 | +
|
| 32 | + async def exit_after_10_seconds() -> None: |
| 33 | + asyncio.sleep(10) |
| 34 | + exit_event.set() |
| 35 | +
|
| 36 | + asyncio.ensure_future(exit_after_10_seconds()) |
| 37 | +
|
| 38 | + async for selected in select(exit_event, other_receiver): |
| 39 | + if selected_from(selected, exit_event): |
| 40 | + break |
| 41 | + if selected_from(selected, other_receiver): |
| 42 | + print(selected.value) |
| 43 | + else: |
| 44 | + assert False, "Unknow receiver selected" |
| 45 | + ``` |
| 46 | + """ |
| 47 | + |
| 48 | + def __init__(self, name: str | None = None) -> None: |
| 49 | + """Create a new instance. |
| 50 | +
|
| 51 | + Args: |
| 52 | + name: The name of the receiver. If `None` the `id(self)` will be used as |
| 53 | + the name. This is only for debugging purposes, it will be shown in the |
| 54 | + string representation of the receiver. |
| 55 | + """ |
| 56 | + self._event: _asyncio.Event = _asyncio.Event() |
| 57 | + """The event that is set when the receiver is ready.""" |
| 58 | + |
| 59 | + self._name: str = name or str(id(self)) |
| 60 | + """The name of the receiver. |
| 61 | +
|
| 62 | + This is for debugging purposes, it will be shown in the string representation |
| 63 | + of the receiver. |
| 64 | + """ |
| 65 | + |
| 66 | + self._is_set: bool = False |
| 67 | + """Whether the receiver is ready to be consumed. |
| 68 | +
|
| 69 | + This is used to differentiate between when the receiver was stopped (the event |
| 70 | + is triggered too) but still there is an event to be consumed and when it was |
| 71 | + stopped but was not explicitly set(). |
| 72 | + """ |
| 73 | + |
| 74 | + self._is_stopped: bool = False |
| 75 | + """Whether the receiver is stopped.""" |
| 76 | + |
| 77 | + @property |
| 78 | + def name(self) -> str: |
| 79 | + """The name of this receiver. |
| 80 | +
|
| 81 | + This is for debugging purposes, it will be shown in the string representation |
| 82 | + of this receiver. |
| 83 | +
|
| 84 | + Returns: |
| 85 | + The name of this receiver. |
| 86 | + """ |
| 87 | + return self._name |
| 88 | + |
| 89 | + @property |
| 90 | + def is_set(self) -> bool: |
| 91 | + """Whether this receiver is set (ready). |
| 92 | +
|
| 93 | + Returns: |
| 94 | + Whether this receiver is set (ready). |
| 95 | + """ |
| 96 | + return self._is_set |
| 97 | + |
| 98 | + @property |
| 99 | + def is_stopped(self) -> bool: |
| 100 | + """Whether this receiver is stopped. |
| 101 | +
|
| 102 | + Returns: |
| 103 | + Whether this receiver is stopped. |
| 104 | + """ |
| 105 | + return self._is_stopped |
| 106 | + |
| 107 | + def stop(self) -> None: |
| 108 | + """Stop this receiver.""" |
| 109 | + self._is_stopped = True |
| 110 | + self._event.set() |
| 111 | + |
| 112 | + def set(self) -> None: |
| 113 | + """Trigger the event (make the receiver ready).""" |
| 114 | + self._is_set = True |
| 115 | + self._event.set() |
| 116 | + |
| 117 | + async def ready(self) -> bool: |
| 118 | + """Wait until this receiver is ready. |
| 119 | +
|
| 120 | + Returns: |
| 121 | + Whether this receiver is still running. |
| 122 | + """ |
| 123 | + if self._is_stopped: |
| 124 | + return False |
| 125 | + await self._event.wait() |
| 126 | + return not self._is_stopped |
| 127 | + |
| 128 | + def consume(self) -> None: |
| 129 | + """Consume the event. |
| 130 | +
|
| 131 | + This makes this receiver wait again until the event is set again. |
| 132 | +
|
| 133 | + Raises: |
| 134 | + ReceiverStoppedError: If this receiver is stopped. |
| 135 | + """ |
| 136 | + if not self._is_set and self._is_stopped: |
| 137 | + raise _exceptions.ReceiverStoppedError(self) |
| 138 | + |
| 139 | + assert self._is_set, "calls to `consume()` must be follow a call to `ready()`" |
| 140 | + |
| 141 | + self._is_set = False |
| 142 | + self._event.clear() |
| 143 | + |
| 144 | + def __str__(self) -> str: |
| 145 | + """Return a string representation of this receiver. |
| 146 | +
|
| 147 | + Returns: |
| 148 | + A string representation of this receiver. |
| 149 | + """ |
| 150 | + return f"{type(self).__name__}({self._name!r})" |
| 151 | + |
| 152 | + def __repr__(self) -> str: |
| 153 | + """Return a string representation of this receiver. |
| 154 | +
|
| 155 | + Returns: |
| 156 | + A string representation of this receiver. |
| 157 | + """ |
| 158 | + return ( |
| 159 | + f"<{type(self).__name__} name={self._name!r} is_set={self.is_set!r} " |
| 160 | + f"is_stopped={self.is_stopped!r}>" |
| 161 | + ) |
0 commit comments