experimental
frequenz.channels.experimental ¤
Experimental channel primitives.
Warning
This package contains experimental channel primitives that are not yet considered stable. They are subject to change without notice, including removal, even in minor updates.
Classes¤
frequenz.channels.experimental.Pipe ¤
Bases: Generic[ChannelMessageT]
A pipe between two channels.
The Pipe
class takes a receiver and a sender and creates a pipe between them
by forwarding all the messages received by the receiver to the sender.
Example
from frequenz.channels import Broadcast, Pipe
channel1: Broadcast[int] = Broadcast(name="channel1")
channel2: Broadcast[int] = Broadcast(name="channel2")
receiver_chan1 = channel1.new_receiver()
sender_chan2 = channel2.new_sender()
async with Pipe(channel2.new_receiver(), channel1.new_sender()):
await sender_chan2.send(10)
assert await receiver_chan1.receive() == 10
Source code in frequenz/channels/experimental/_pipe.py
Functions¤
__aenter__
async
¤
__aenter__() -> Pipe[ChannelMessageT]
__aexit__
async
¤
__aexit__(
_exc_type: Type[BaseException],
_exc: BaseException,
_tb: Any,
) -> None
__init__ ¤
__init__(
receiver: Receiver[ChannelMessageT],
sender: Sender[ChannelMessageT],
) -> None
Create a new pipe between two channels.
PARAMETER | DESCRIPTION |
---|---|
receiver
|
The receiver channel.
TYPE:
|
sender
|
The sender channel.
TYPE:
|
Source code in frequenz/channels/experimental/_pipe.py
start
async
¤
frequenz.channels.experimental.RelaySender ¤
Bases: Generic[SenderMessageT_contra]
, Sender[SenderMessageT_contra]
A Sender for sending messages to multiple senders.
The RelaySender
class takes multiple senders and forwards all the messages sent to
it, to the senders it was created with.
Example
from frequenz.channels import Broadcast
from frequenz.channels.experimental import RelaySender
channel1: Broadcast[int] = Broadcast(name="channel1")
channel2: Broadcast[int] = Broadcast(name="channel2")
receiver1 = channel1.new_receiver()
receiver2 = channel2.new_receiver()
tee_sender = RelaySender(channel1.new_sender(), channel2.new_sender())
await tee_sender.send(5)
assert await receiver1.receive() == 5
assert await receiver2.receive() == 5
Source code in frequenz/channels/experimental/_relay_sender.py
Functions¤
__init__ ¤
__init__(*senders: Sender[SenderMessageT_contra]) -> None
Create a new RelaySender.
PARAMETER | DESCRIPTION |
---|---|
*senders
|
The senders to send messages to.
TYPE:
|
send
async
¤
send(message: SenderMessageT_contra) -> None
Send a message.
PARAMETER | DESCRIPTION |
---|---|
message
|
The message to be sent.
TYPE:
|
frequenz.channels.experimental.WithPrevious ¤
Bases: Generic[ChannelMessageT]
A composable predicate to build predicates that can use also the previous message.
This predicate can be used to filter messages based on a custom condition on the previous and current messages. This can be useful in cases where you want to process messages only if they satisfy a particular condition with respect to the previous message.
Receiving only messages that are different from the previous one.
from frequenz.channels import Broadcast
from frequenz.channels.experimental import WithPrevious
channel = Broadcast[int](name="example")
receiver = channel.new_receiver().filter(WithPrevious(lambda old, new: old != new))
sender = channel.new_sender()
# This message will be received as it is the first message.
await sender.send(1)
assert await receiver.receive() == 1
# This message will be skipped as it equals to the previous one.
await sender.send(1)
# This message will be received as it is a different from the previous one.
await sender.send(0)
assert await receiver.receive() == 0
Receiving only messages if they are bigger than the previous one.
```python from frequenz.channels import Broadcast from frequenz.channels.experimental import WithPrevious
channel = Broadcastint receiver = channel.new_receiver().filter( WithPrevious(lambda old, new: new > old, first_is_true=False) ) sender = channel.new_sender()
This message will skipped as first_is_true is False.¤
await sender.send(1)
This message will be received as it is bigger than the previous one (1).¤
await sender.send(2) assert await receiver.receive() == 2
This message will be skipped as it is smaller than the previous one (1).¤
await sender.send(0)
This message will be skipped as it is not bigger than the previous one (0).¤
await sender.send(0)
This message will be received as it is bigger than the previous one (0).¤
await sender.send(1) assert await receiver.receive() == 1
This message will be received as it is bigger than the previous one (1).¤
await sender.send(2) assert await receiver.receive() == 2
Source code in frequenz/channels/experimental/_with_previous.py
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 |
|
Functions¤
__call__ ¤
__call__(message: ChannelMessageT) -> bool
Return whether message
is the first one received or different from the previous one.
Source code in frequenz/channels/experimental/_with_previous.py
__init__ ¤
__init__(
predicate: Callable[
[ChannelMessageT, ChannelMessageT], bool
],
/,
*,
first_is_true: bool = True,
) -> None
Initialize this instance.
PARAMETER | DESCRIPTION |
---|---|
predicate
|
A callable that takes two arguments, the previous message and the current message, and returns a boolean indicating whether the current message should be received.
TYPE:
|
first_is_true
|
Whether the first message should be considered as satisfying
the predicate. Defaults to
TYPE:
|