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:
-
Error: Base class for all errors in this library.
-
ChannelError: Base class for all errors related to channels.
-
ChannelClosedError: Error raised when trying to operate (send, receive, etc.) through a closed channel.
-
SenderError: Base class for all errors related to senders.
-
ReceiverError: Base class for all errors related to receivers.
-
ReceiverStoppedError: A receiver stopped producing messages.
-
ReceiverInvalidatedError: A receiver is not longer valid (for example if it was converted into a peekable.
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
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 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 |
|
Attributes¤
client_handle: Bidirectional.Handle[T, U]
property
¤
Get a Handle
for the client side to use.
RETURNS | DESCRIPTION |
---|---|
Handle[T, U]
|
Object to send/receive messages with. |
service_handle: Bidirectional.Handle[U, T]
property
¤
Get a Handle
for the service side to use.
RETURNS | DESCRIPTION |
---|---|
Handle[U, T]
|
Object to send/receive messages with. |
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
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 109 110 |
|
__init__(channel, sender, receiver)
¤Create a Bidirectional.Handle
instance.
PARAMETER | DESCRIPTION |
---|---|
channel |
The underlying channel.
TYPE:
|
sender |
A sender to send values with.
TYPE:
|
receiver |
A receiver to receive values from.
TYPE:
|
Source code in frequenz/channels/_bidirectional.py
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
|
consume()
¤Return the latest value once _ready
is complete.
RETURNS | DESCRIPTION |
---|---|
W
|
The next value that was received. |
RAISES | DESCRIPTION |
---|---|
ReceiverStoppedError
|
if there is some problem with the receiver. |
ReceiverError
|
if there is some problem with the receiver. |
noqa: DAR401 err (https://github.com/terrencepreilly/darglint/issues/181)¤
Source code in frequenz/channels/_bidirectional.py
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 |
|
ready()
async
¤Wait until the receiver is ready with a value or an error.
Once a call to ready()
has finished, the value should be read with
a call to consume()
(receive()
or iterated over). The receiver will
remain ready (this method will return immediately) until it is
consumed.
RETURNS | DESCRIPTION |
---|---|
bool
|
Whether the receiver is still active. |
Source code in frequenz/channels/_bidirectional.py
71 72 73 74 75 76 77 78 79 80 81 82 |
|
send(msg)
async
¤Send a value to the other side.
PARAMETER | DESCRIPTION |
---|---|
msg |
The value to send.
TYPE:
|
RAISES | DESCRIPTION |
---|---|
SenderError
|
if the underlying channel was closed. A ChannelClosedError is set as the cause. |
Source code in frequenz/channels/_bidirectional.py
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 |
|
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
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
|
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
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 |
|
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
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
|
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
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
|
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
144 145 146 147 148 149 150 151 152 153 154 |
|
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
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
|
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
111 112 113 114 115 116 117 |
|
frequenz.channels.ChannelClosedError
¤
Bases: ChannelError
Error raised when trying to operate on a closed channel.
Source code in frequenz/channels/_exceptions.py
48 49 50 51 52 53 54 55 56 57 |
|
Functions¤
__init__(channel)
¤
Create a ChannelClosedError
instance.
PARAMETER | DESCRIPTION |
---|---|
channel |
A reference to the channel that was closed.
TYPE:
|
Source code in frequenz/channels/_exceptions.py
51 52 53 54 55 56 57 |
|
frequenz.channels.ChannelError
¤
Bases: Error
An error produced in a channel.
All exceptions generated by channels inherit from this exception.
Source code in frequenz/channels/_exceptions.py
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
|
Functions¤
__init__(message, channel)
¤
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/_exceptions.py
37 38 39 40 41 42 43 44 45 |
|
frequenz.channels.Error
¤
Bases: RuntimeError
Base error.
All exceptions generated by this library inherit from this exception.
Source code in frequenz/channels/_exceptions.py
16 17 18 19 20 21 22 23 24 25 26 27 28 |
|
Functions¤
__init__(message)
¤
Create a ChannelError instance.
PARAMETER | DESCRIPTION |
---|---|
message |
An error message.
TYPE:
|
Source code in frequenz/channels/_exceptions.py
22 23 24 25 26 27 28 |
|
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
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
|
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
147 148 149 150 151 152 153 154 |
|
frequenz.channels.Receiver
¤
A channel Receiver.
Source code in frequenz/channels/_base_classes.py
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 |
|
Functions¤
__aiter__()
¤
Initialize the async iterator over received values.
RETURNS | DESCRIPTION |
---|---|
Receiver[T]
|
|
Source code in frequenz/channels/_base_classes.py
78 79 80 81 82 83 84 |
|
__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 frequenz/channels/_base_classes.py
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
|
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 |
---|---|
ReceiverStoppedError
|
if the receiver stopped producing messages. |
ReceiverError
|
if there is some problem with the receiver. |
Source code in frequenz/channels/_base_classes.py
64 65 66 67 68 69 70 71 72 73 74 75 76 |
|
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
126 127 128 129 130 131 132 133 134 135 136 |
|
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
115 116 117 118 119 120 121 122 123 124 |
|
ready()
abstractmethod
async
¤
Wait until the receiver is ready with a value or an error.
Once a call to ready()
has finished, the value should be read with
a call to consume()
(receive()
or iterated over). The receiver will
remain ready (this method will return immediately) until it is
consumed.
RETURNS | DESCRIPTION |
---|---|
bool
|
Whether the receiver is still active. |
Source code in frequenz/channels/_base_classes.py
51 52 53 54 55 56 57 58 59 60 61 62 |
|
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. |
noqa: DAR401 cause (https://github.com/terrencepreilly/darglint/issues/181)¤
Source code in frequenz/channels/_base_classes.py
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 |
|
frequenz.channels.ReceiverError
¤
An error produced in a Receiver.
All exceptions generated by receivers inherit from this exception.
Source code in frequenz/channels/_exceptions.py
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
|
Functions¤
__init__(message, receiver)
¤
Create an instance.
PARAMETER | DESCRIPTION |
---|---|
message |
An error message.
TYPE:
|
receiver |
The Receiver where the error happened.
TYPE:
|
Source code in frequenz/channels/_exceptions.py
84 85 86 87 88 89 90 91 92 93 |
|
frequenz.channels.ReceiverInvalidatedError
¤
Bases: ReceiverError[T]
The Receiver was invalidated.
This happens when the Receiver is converted into a Peekable.
Source code in frequenz/channels/_exceptions.py
109 110 111 112 113 114 115 |
|
frequenz.channels.ReceiverStoppedError
¤
Bases: ReceiverError[T]
The Receiver stopped producing messages.
Source code in frequenz/channels/_exceptions.py
96 97 98 99 100 101 102 103 104 105 106 |
|
Functions¤
__init__(receiver)
¤
Create an instance.
PARAMETER | DESCRIPTION |
---|---|
receiver |
The Receiver where the error happened.
TYPE:
|
Source code in frequenz/channels/_exceptions.py
99 100 101 102 103 104 105 106 |
|
frequenz.channels.Sender
¤
A channel Sender.
Source code in frequenz/channels/_base_classes.py
17 18 19 20 21 22 23 24 25 26 27 28 29 |
|
Functions¤
send(msg)
abstractmethod
async
¤
Send a message to the channel.
PARAMETER | DESCRIPTION |
---|---|
msg |
The message to be sent.
TYPE:
|
RAISES | DESCRIPTION |
---|---|
SenderError
|
if there was an error sending the message. |
Source code in frequenz/channels/_base_classes.py
20 21 22 23 24 25 26 27 28 29 |
|
frequenz.channels.SenderError
¤
An error produced in a Sender.
All exceptions generated by senders inherit from this exception.
Source code in frequenz/channels/_exceptions.py
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
|
Functions¤
__init__(message, sender)
¤
Create an instance.
PARAMETER | DESCRIPTION |
---|---|
message |
An error message.
TYPE:
|
sender |
The Sender where the error happened.
TYPE:
|
Source code in frequenz/channels/_exceptions.py
66 67 68 69 70 71 72 73 74 75 |
|