event
frequenz.channels.event ¤
A receiver that can be made ready directly.
Tip
Read the Event
documentation for more
information.
This module contains the following:
Event
: A receiver that can be made ready directly.
Classes¤
frequenz.channels.event.Event ¤
Bases: Receiver[None]
A receiver that can be made ready directly.
Usage¤
There are cases where it is useful to be able to send a signal to
a select()
loop, for example, to stop a loop from
outside the loop itself.
To do that, you can use an Event
receiver and
call set()
on it when you want to make it
ready.
Stopping¤
The receiver will be re-activated (will keep blocking) after the current set
event is received. To stop the receiver completely, you can call
stop()
.
Example¤
Exit after printing the first 5 numbers
import asyncio
from frequenz.channels import Anycast, select, selected_from
from frequenz.channels.event import Event
channel: Anycast[int] = Anycast(name="channel")
receiver = channel.new_receiver()
sender = channel.new_sender()
stop_event = Event(name="stop")
async def do_work() -> None:
async for selected in select(receiver, stop_event):
if selected_from(selected, receiver):
print(selected.value)
elif selected_from(selected, stop_event):
print("Stop event triggered")
stop_event.stop()
break
async def send_stuff() -> None:
for i in range(10):
if stop_event.is_stopped:
break
await asyncio.sleep(1)
await sender.send(i)
async def main() -> None:
async with asyncio.TaskGroup() as task_group:
task_group.create_task(do_work(), name="do_work")
task_group.create_task(send_stuff(), name="send_stuff")
await asyncio.sleep(5.5)
stop_event.set()
asyncio.run(main())
Source code in src/frequenz/channels/event.py
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
|
Attributes¤
is_set
property
¤
Whether this receiver is set (ready).
RETURNS | DESCRIPTION |
---|---|
bool
|
Whether this receiver is set (ready). |
is_stopped
property
¤
Whether this receiver is stopped.
RETURNS | DESCRIPTION |
---|---|
bool
|
Whether this receiver is stopped. |
name
property
¤
The name of this receiver.
This is for debugging purposes, it will be shown in the string representation of this receiver.
RETURNS | DESCRIPTION |
---|---|
str
|
The name of this receiver. |
Functions¤
__aiter__ ¤
Initialize the async iterator over received values.
RETURNS | DESCRIPTION |
---|---|
Self
|
|
__anext__
async
¤
Await the next value in the async iteration over received values.
RETURNS | DESCRIPTION |
---|---|
_T
|
The next value received. |
RAISES | DESCRIPTION |
---|---|
StopAsyncIteration
|
if the receiver stopped producing messages. |
ReceiverError
|
if there is some problem with the receiver. |
Source code in src/frequenz/channels/_receiver.py
__init__ ¤
Create a new instance.
PARAMETER | DESCRIPTION |
---|---|
name |
The name of the receiver. If
TYPE:
|
Source code in src/frequenz/channels/event.py
__repr__ ¤
Return a string representation of this receiver.
RETURNS | DESCRIPTION |
---|---|
str
|
A string representation of this receiver. |
Source code in src/frequenz/channels/event.py
__str__ ¤
Return a string representation of this receiver.
RETURNS | DESCRIPTION |
---|---|
str
|
A string representation of this receiver. |
consume ¤
Consume the event.
This makes this receiver wait again until the event is set again.
RAISES | DESCRIPTION |
---|---|
ReceiverStoppedError
|
If this receiver is stopped. |
Source code in src/frequenz/channels/event.py
map ¤
ready
async
¤
Wait until this receiver is ready.
RETURNS | DESCRIPTION |
---|---|
bool
|
Whether this receiver is still running. |
receive
async
¤
Receive a message from the channel.
RETURNS | DESCRIPTION |
---|---|
_T
|
The received message. |
RAISES | DESCRIPTION |
---|---|
ReceiverStoppedError
|
if there is some problem with the receiver. |
ReceiverError
|
if there is some problem with the receiver. |