channels
frequenz.channels
¤
Frequenz Channels.
This package contains channel implementations.
Channels:
-
Anycast: A channel that supports multiple senders and multiple receivers. A message sent through a sender will be received by exactly one receiver.
-
Bidirectional: A channel providing a
client
and aservice
handle to send and receive bidirectionally. -
Broadcast: A channel to broadcast messages from multiple senders to multiple receivers. Each message sent through any of the senders is received by all of the receivers.
Other base classes:
-
Peekable: An object to allow users to get a peek at the latest value in the channel, without consuming anything.
-
Receiver: An object that can wait for and consume messages from a channel.
-
Sender: An object that can send messages to a channel.
Utilities:
- util: A module with utilities, like special receivers that implement timers, file watchers, merge receivers, or wait for messages in multiple channels.
Exception classes:
-
ChannelError: Base class for all errors related to channels.
-
ChannelClosedError: Error raised when trying to operate (send, receive, etc.) through a closed channel.
Classes¤
frequenz.channels.Anycast
¤
Bases: Generic[T]
A channel for sending data across async tasks.
Anycast channels support multiple senders and multiple receivers. A message sent through a sender will be received by exactly one receiver.
In cases where each message need to be received by every receiver, a Broadcast channel may be used.
Uses an deque internally, so Anycast channels are not thread-safe.
When there are multiple channel receivers, they can be awaited simultaneously using Select, Merge or MergeNamed.
Example
async def send(sender: channel.Sender) -> None:
while True:
next = random.randint(3, 17)
print(f"sending: {next}")
await sender.send(next)
async def recv(id: int, receiver: channel.Receiver) -> None:
while True:
next = await receiver.receive()
print(f"receiver_{id} received {next}")
await asyncio.sleep(0.1) # sleep (or work) with the data
acast = channel.Anycast()
sender = acast.new_sender()
receiver_1 = acast.new_receiver()
asyncio.create_task(send(sender))
await recv(1, receiver_1)
Check the tests
and benchmarks
directories for more examples.
Source code in frequenz/channels/_anycast.py
18 19 20 21 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 |
|
Functions¤
__init__(maxsize=10)
¤
Create an Anycast channel.
PARAMETER | DESCRIPTION |
---|---|
maxsize |
Size of the channel's buffer.
TYPE:
|
Source code in frequenz/channels/_anycast.py
64 65 66 67 68 69 70 71 72 73 74 |
|
close()
async
¤
Close the channel.
Any further attempts to send() data
will return False
.
Receivers will still be able to drain the pending items on the channel,
but after that, subsequent
receive() calls will return None
immediately.
Source code in frequenz/channels/_anycast.py
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
|
new_receiver()
¤
Create a new receiver.
RETURNS | DESCRIPTION |
---|---|
Receiver[T]
|
A Receiver instance attached to the Anycast channel. |
Source code in frequenz/channels/_anycast.py
102 103 104 105 106 107 108 |
|
new_sender()
¤
Create a new sender.
RETURNS | DESCRIPTION |
---|---|
Sender[T]
|
A Sender instance attached to the Anycast channel. |
Source code in frequenz/channels/_anycast.py
94 95 96 97 98 99 100 |
|
frequenz.channels.Bidirectional
¤
Bases: Generic[T, U]
A wrapper class for simulating bidirectional channels.
Source code in frequenz/channels/_bidirectional.py
17 18 19 20 21 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 |
|
Classes¤
Handle
¤
A handle to a Bidirectional instance.
It can be used to send/receive values between the client and service.
Source code in frequenz/channels/_bidirectional.py
20 21 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 |
|
__init__(sender, receiver)
¤Create a Bidirectional.Handle
instance.
PARAMETER | DESCRIPTION |
---|---|
sender |
A sender to send values with.
TYPE:
|
receiver |
A receiver to receive values from.
TYPE:
|
Source code in frequenz/channels/_bidirectional.py
26 27 28 29 30 31 32 33 34 |
|
consume()
¤Return the latest value once _ready
is complete.
RETURNS | DESCRIPTION |
---|---|
W
|
The next value that was received. |
Source code in frequenz/channels/_bidirectional.py
51 52 53 54 55 56 57 |
|
ready()
async
¤Wait until the receiver is ready with a value.
Source code in frequenz/channels/_bidirectional.py
47 48 49 |
|
send(msg)
async
¤Send a value to the other side.
PARAMETER | DESCRIPTION |
---|---|
msg |
The value to send.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
bool
|
Whether the send was successful or not. |
Source code in frequenz/channels/_bidirectional.py
36 37 38 39 40 41 42 43 44 45 |
|
Functions¤
__init__(client_id, service_id)
¤
Create a Bidirectional
instance.
PARAMETER | DESCRIPTION |
---|---|
client_id |
A name for the client, used to name the channels.
TYPE:
|
service_id |
A name for the service end of the channels.
TYPE:
|
Source code in frequenz/channels/_bidirectional.py
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
|
client_handle()
property
¤
Get a Handle
for the client side to use.
RETURNS | DESCRIPTION |
---|---|
Bidirectional.Handle[T, U]
|
Object to send/receive messages with. |
Source code in frequenz/channels/_bidirectional.py
81 82 83 84 85 86 87 88 |
|
service_handle()
property
¤
Get a Handle
for the service side to use.
RETURNS | DESCRIPTION |
---|---|
Bidirectional.Handle[U, T]
|
Object to send/receive messages with. |
Source code in frequenz/channels/_bidirectional.py
90 91 92 93 94 95 96 97 |
|
frequenz.channels.Broadcast
¤
Bases: Generic[T]
A channel to broadcast messages to multiple receivers.
Broadcast
channels can have multiple senders and multiple receivers. Each
message sent through any of the senders is received by all of the
receivers.
Internally, a broadcast receiver's buffer is implemented with just
append/pop operations on either side of a deque, which
are thread-safe. Because of this, Broadcast
channels are thread-safe.
When there are multiple channel receivers, they can be awaited simultaneously using Select, Merge or MergeNamed.
Example
async def send(sender: channel.Sender) -> None:
while True:
next = random.randint(3, 17)
print(f"sending: {next}")
await sender.send(next)
async def recv(id: int, receiver: channel.Receiver) -> None:
while True:
next = await receiver.receive()
print(f"receiver_{id} received {next}")
await asyncio.sleep(0.1) # sleep (or work) with the data
bcast = channel.Broadcast()
sender = bcast.new_sender()
receiver_1 = bcast.new_receiver()
asyncio.create_task(send(sender))
await recv(1, receiver_1)
Check the tests
and benchmarks
directories for more examples.
Source code in frequenz/channels/_broadcast.py
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 |
|
Functions¤
__init__(name, resend_latest=False)
¤
Create a Broadcast channel.
PARAMETER | DESCRIPTION |
---|---|
name |
A name for the broadcast channel, typically based on the type of data sent through it. Used to identify the channel in the logs.
TYPE:
|
resend_latest |
When True, every time a new receiver is created with
TYPE:
|
Source code in frequenz/channels/_broadcast.py
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
|
close()
async
¤
Close the Broadcast channel.
Any further attempts to send() data
will return False
.
Receivers will still be able to drain the pending items on their queues,
but after that, subsequent
receive() calls will return None
immediately.
Source code in frequenz/channels/_broadcast.py
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
|
new_peekable()
¤
Create a new Peekable for the broadcast channel.
A Peekable provides a peek() method that allows the user to get a peek at the latest value in the channel, without consuming anything.
RETURNS | DESCRIPTION |
---|---|
Peekable[T]
|
A Peekable to peek into the broadcast channel with. |
Source code in frequenz/channels/_broadcast.py
139 140 141 142 143 144 145 146 147 148 149 |
|
new_receiver(name=None, maxsize=50)
¤
Create a new broadcast receiver.
Broadcast receivers have their own buffer, and when messages are not being consumed fast enough and the buffer fills up, old messages will get dropped just in this receiver.
PARAMETER | DESCRIPTION |
---|---|
name |
A name to identify the receiver in the logs. |
maxsize |
Size of the receiver's buffer.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Receiver[T]
|
A Receiver instance attached to the broadcast channel. |
Source code in frequenz/channels/_broadcast.py
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
|
new_sender()
¤
Create a new broadcast sender.
RETURNS | DESCRIPTION |
---|---|
Sender[T]
|
A Sender instance attached to the broadcast channel. |
Source code in frequenz/channels/_broadcast.py
106 107 108 109 110 111 112 |
|
frequenz.channels.ChannelClosedError
¤
Bases: ChannelError
Error raised when trying to operate on a closed channel.
Source code in frequenz/channels/_base_classes.py
32 33 34 35 36 37 38 39 40 41 |
|
Functions¤
__init__(channel=None)
¤
Create a ChannelClosedError
instance.
PARAMETER | DESCRIPTION |
---|---|
channel |
A reference to the channel that was closed.
TYPE:
|
Source code in frequenz/channels/_base_classes.py
35 36 37 38 39 40 41 |
|
frequenz.channels.ChannelError
¤
Bases: RuntimeError
Base channel error.
All exceptions generated by channels inherit from this exception.
Source code in frequenz/channels/_base_classes.py
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
|
Functions¤
__init__(message, channel=None)
¤
Create a ChannelError instance.
PARAMETER | DESCRIPTION |
---|---|
message |
An error message.
TYPE:
|
channel |
A reference to the channel that encountered the error.
TYPE:
|
Source code in frequenz/channels/_base_classes.py
21 22 23 24 25 26 27 28 29 |
|
frequenz.channels.Peekable
¤
A channel peekable.
A Peekable provides a peek() method that allows the user to get a peek at the latest value in the channel, without consuming anything.
Source code in frequenz/channels/_base_classes.py
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
|
Functions¤
peek()
abstractmethod
¤
Return the latest value that was sent to the channel.
RETURNS | DESCRIPTION |
---|---|
Optional[T]
|
The latest value received by the channel, and |
Source code in frequenz/channels/_base_classes.py
157 158 159 160 161 162 163 164 |
|
frequenz.channels.Receiver
¤
A channel Receiver.
Source code in frequenz/channels/_base_classes.py
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 |
|
Functions¤
__aiter__()
¤
Initialize the async iterator over received values.
RETURNS | DESCRIPTION |
---|---|
Receiver[T]
|
|
Source code in frequenz/channels/_base_classes.py
102 103 104 105 106 107 108 |
|
__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 underlying channel is closed. |
Source code in frequenz/channels/_base_classes.py
63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
|
consume()
abstractmethod
¤
Return the latest value once ready()
is complete.
ready()
must be called before each call to consume()
.
RETURNS | DESCRIPTION |
---|---|
T
|
The next value received. |
RAISES | DESCRIPTION |
---|---|
ChannelClosedError
|
if the underlying channel is closed. |
Source code in frequenz/channels/_base_classes.py
89 90 91 92 93 94 95 96 97 98 99 100 |
|
into_peekable()
¤
Convert the Receiver
implementation into a Peekable
.
Once this function has been called, the receiver will no longer be
usable, and calling receive
on the receiver will raise an exception.
RAISES | DESCRIPTION |
---|---|
NotImplementedError
|
when a |
Source code in frequenz/channels/_base_classes.py
136 137 138 139 140 141 142 143 144 145 146 |
|
map(call)
¤
Return a receiver with call
applied on incoming messages.
PARAMETER | DESCRIPTION |
---|---|
call |
function to apply on incoming messages.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Receiver[U]
|
A |
Source code in frequenz/channels/_base_classes.py
125 126 127 128 129 130 131 132 133 134 |
|
ready()
abstractmethod
async
¤
Wait until the receiver is ready with a value.
Once a call to ready()
has finished, the value should be read with a call to
consume()
.
RAISES | DESCRIPTION |
---|---|
ChannelClosedError
|
if the underlying channel is closed. |
Source code in frequenz/channels/_base_classes.py
78 79 80 81 82 83 84 85 86 87 |
|
receive()
async
¤
Receive a message from the channel.
RAISES | DESCRIPTION |
---|---|
ChannelClosedError
|
if the underlying channel is closed. |
RETURNS | DESCRIPTION |
---|---|
T
|
The received message. |
Source code in frequenz/channels/_base_classes.py
110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
|
frequenz.channels.Sender
¤
A channel Sender.
Source code in frequenz/channels/_base_classes.py
44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
|
Functions¤
send(msg)
abstractmethod
async
¤
Send a message to the channel.
PARAMETER | DESCRIPTION |
---|---|
msg |
The message to be sent.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
bool
|
Whether the message was sent, based on whether the channel is open or not. |
Source code in frequenz/channels/_base_classes.py
47 48 49 50 51 52 53 54 55 56 57 |
|