Skip to content

Commit 23bab61

Browse files
committed
Add broadcast function and deprecate Broadcast class
The `broadcast` function would only return a sender and a receiver from an auto-closing channel. Signed-off-by: Sahas Subramanian <sahas.subramanian@proton.me>
1 parent 9082d38 commit 23bab61

2 files changed

Lines changed: 51 additions & 2 deletions

File tree

src/frequenz/channels/__init__.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@
8080
"""
8181

8282
from ._anycast import Anycast
83-
from ._broadcast import Broadcast
83+
from ._broadcast import Broadcast, broadcast
8484
from ._exceptions import ChannelClosedError, ChannelError, Error
8585
from ._generic import (
8686
ChannelMessageT,
@@ -100,14 +100,23 @@
100100
select,
101101
selected_from,
102102
)
103-
from ._sender import Sender, SenderError
103+
from ._sender import (
104+
ClonableSender,
105+
ClonableSubscribableSender,
106+
Sender,
107+
SenderClosedError,
108+
SenderError,
109+
SubscribableSender,
110+
)
104111

105112
__all__ = [
106113
"Anycast",
107114
"Broadcast",
108115
"ChannelClosedError",
109116
"ChannelError",
110117
"ChannelMessageT",
118+
"ClonableSender",
119+
"ClonableSubscribableSender",
111120
"Error",
112121
"ErroredChannelT_co",
113122
"LatestValueCache",
@@ -120,10 +129,13 @@
120129
"SelectError",
121130
"Selected",
122131
"Sender",
132+
"SenderClosedError",
123133
"SenderError",
124134
"SenderMessageT_co",
125135
"SenderMessageT_contra",
136+
"SubscribableSender",
126137
"UnhandledSelectedError",
138+
"broadcast",
127139
"merge",
128140
"select",
129141
"selected_from",

src/frequenz/channels/_broadcast.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,43 @@
2121
_logger = logging.getLogger(__name__)
2222

2323

24+
def broadcast(
25+
message_type: type[ChannelMessageT], # pylint: disable=unused-argument
26+
/,
27+
*,
28+
name: str,
29+
resend_latest: bool = False,
30+
) -> tuple[ClonableSubscribableSender[ChannelMessageT], Receiver[ChannelMessageT]]:
31+
"""Create a new Broadcast channel and return a sender and a receiver attached to it.
32+
33+
The channel will be automatically closed when all senders or all receivers
34+
are closed.
35+
36+
Args:
37+
message_type: The type of messages that will be sent through this channel. This
38+
is only for type checking purposes, it is not used at runtime.
39+
name: The name of the channel. This is for logging purposes, and it will be
40+
shown in the string representation of the channel.
41+
resend_latest: When True, every time a new receiver is created with
42+
`new_receiver`, the last message seen by the channel will be sent to the
43+
new receiver automatically. This allows new receivers on slow streams to
44+
get the latest message as soon as they are created, without having to
45+
wait for the next message on the channel to arrive. It is safe to be
46+
set in data/reporting channels, but is not recommended for use in
47+
channels that stream control instructions.
48+
49+
Returns:
50+
A tuple of a sender and a receiver attached to the created channel.
51+
"""
52+
channel = Broadcast[ChannelMessageT](
53+
name=name, resend_latest=resend_latest, auto_close=True
54+
)
55+
return channel.new_sender(), channel.new_receiver()
56+
57+
58+
@deprecated(
59+
"Please use the `broadcast` function to create a Broadcast channel instead."
60+
)
2461
class Broadcast( # pylint: disable=too-many-instance-attributes
2562
Generic[ChannelMessageT]
2663
):

0 commit comments

Comments
 (0)