Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
da0ee39
Wake waiting broadcast receivers when they are closed
shsms Oct 19, 2026
90fb133
Stop reporting closed broadcast receivers as ready
shsms Oct 21, 2026
3b7e4da
Keep broadcast channels open when their receivers are gone
shsms Oct 21, 2026
4a16c77
Refuse to clone closed broadcast senders
shsms Oct 19, 2026
788a90c
Apply BroadcastChannel receiver options to subscribed receivers
shsms Oct 19, 2026
6e73966
Add has_receivers and has_senders to broadcast endpoints
shsms Oct 19, 2026
8d0b94e
Stop BroadcastChannel from emitting a deprecation warning
shsms Oct 19, 2026
e59cc98
Point the Broadcast docs to BroadcastChannel
shsms Oct 31, 2026
3049de9
Document how BroadcastChannel closes itself
shsms Oct 31, 2026
28fcb62
Correct the broadcast docs on closing
shsms Oct 31, 2026
c109906
Show BroadcastChannel in the user guide and README
shsms Oct 19, 2026
81897bd
Use BroadcastChannel in docstring examples
shsms Oct 19, 2026
93e5bff
Close a pipe's ends when it stops on its own
shsms Oct 21, 2026
d8f894a
Let a pipe keep sending after a failed send
shsms Nov 2, 2026
42053e0
Update release notes
shsms Oct 19, 2026
3073f45
Add has_receivers/has_senders and fix BroadcastChannel auto-close (#565)
shsms Nov 5, 2026
786c842
Merge v1.x.x into v2.x.x
llucax Nov 13, 2026
64f5d46
Use `buffer_size` in the tests of the subscribed receiver options
llucax Nov 13, 2026
f9559f6
Make the broadcast channel options keyword-only
llucax Nov 13, 2026
0f0ec60
Store the loop of waiting receivers instead of reading `Event._loop`
llucax Nov 13, 2026
af81c74
Rename the internal `limit` of broadcast receivers to `buffer_size`
llucax Nov 13, 2026
f2eaab1
Update the release notes for the merge of v1.x.x
llucax Nov 13, 2026
8260df4
Merge `v1.x.x` into `v2.x.x` (#605)
llucax Nov 13, 2026
68a0f0f
Remove the deprecated `limit` argument of broadcast channels
llucax Nov 13, 2026
126cd22
Remove the deprecated `Timer` alias
llucax Nov 13, 2026
baadaba
Remove the deprecated symbols of `experimental`
llucax Nov 13, 2026
0672ebe
Remove the deprecated `close()` of channels
llucax Nov 13, 2026
b059ac2
Make `Receiver.close()` abstract
llucax Nov 13, 2026
d9441c2
Make `Receiver.close()` abstract (#609)
llucax Nov 16, 2026
ff646f9
Remove what v1.13.0 deprecated (#606)
llucax Nov 16, 2026
1774ed6
Add `TransientSenderError`
llucax Nov 13, 2026
b082030
Rename `NoReceiversError` to `SenderHasNoReceiversError`
llucax Nov 13, 2026
446a1ee
Update the release notes for the new sender errors
llucax Nov 13, 2026
ff39406
Add `TransientSenderError` and rename `NoReceiversError` to `SenderHa…
llucax Nov 20, 2026
11e2720
Use `BroadcastChannel` where `Anycast` was only an example
llucax Nov 20, 2026
bbd0be0
Remove `Anycast`
llucax Nov 20, 2026
496ff9d
Remove `Anycast` (#614)
llucax Nov 20, 2026
2e53357
Deprecate `Broadcast` with `@deprecated`, not an alias
llucax Nov 13, 2026
07d9f57
Drop the release notes entry about `Broadcast`
llucax Nov 13, 2026
56c9b95
Deprecate `Broadcast` with `@deprecated`, not an alias (#610)
llucax Nov 20, 2026
12ab646
build(deps-dev): Bump the minor group across 1 directory with 10 updates
dependabot[bot] Nov 20, 2026
40324b5
Bump pymdownx-superfence-filter-lines to 1.0.0
llucax Nov 20, 2026
4a78e93
Silence pylint's abstract-method warning in a receiver test
llucax Nov 20, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/bug.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ body:
- Documentation (part:docs)
- Unit, integration and performance tests (part:tests)
- Build script, CI, dependencies, etc. (part:tooling)
- Channels, `Broadcast`, `Anycast`, etc. (part:channels)
- Channels, `BroadcastChannel`, etc. (part:channels)
- Core types (`Sender`, `Receiver`, exceptions, etc.) (part:core)
- Experimental features (the `experimental` package) (part:experimental)
- Utilities (`Event`, `FileWatcher`, `Timer`, etc.) (part:utilities)
Expand Down
1 change: 0 additions & 1 deletion .github/labeler.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
"part:channels":
- changed-files:
- any-glob-to-any-file:
- "src/frequenz/channels/_anycast.py"
- "src/frequenz/channels/_broadcast.py"

"part:core":
Expand Down
53 changes: 22 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,11 @@ python3 -m pip install frequenz-channels
```python
import asyncio

from frequenz.channels import Anycast
from frequenz.channels import BroadcastChannel


async def main() -> None:
hello_channel = Anycast[str](name="hello-world-channel")
sender = hello_channel.new_sender()
receiver = hello_channel.new_receiver()
sender, receiver = BroadcastChannel[str](name="hello-world-channel")

await sender.send("Hello World!")
message = await receiver.receive()
Expand All @@ -100,8 +98,7 @@ from enum import Enum, auto
from typing import assert_never

from frequenz.channels import (
Anycast,
Broadcast,
BroadcastChannel,
Receiver,
Sender,
merge,
Expand Down Expand Up @@ -151,6 +148,7 @@ async def send(
await control_reply.send(Reply(ReplyCommand.PONG, str(sender)))
case _ as unknown:
assert_never(unknown)
await sender.aclose()
print(f"{sender}: Finished")


Expand All @@ -159,13 +157,16 @@ async def receive(
control_command: Receiver[Command],
control_reply: Sender[Reply],
) -> None:
"""Receive data from multiple channels, until no more data is received for 2 seconds."""
"""Receive data from multiple channels, until they're closed or idle for 2 seconds."""
print("receive: Starting")
timer = MonotonicTimer(timedelta(seconds=2.0), SkipMissedAndDrift())
print(f"{timer=}")
merged = merge(*receivers)
async for selected in select(merged, timer, control_command):
if selected_from(selected, merged):
if selected.was_stopped:
print("receive: All data channels were closed")
break
message = selected.message
print(f"receive: Received {message=}")
timer.reset()
Expand All @@ -191,51 +192,41 @@ async def receive(


async def main() -> None:
data_channel_1 = Anycast[str](name="data-channel-1")
data_channel_2 = Anycast[str](name="data-channel-2")
command_channel = Broadcast[Command](name="control-channel") # (1)!
reply_channel = Anycast[Reply](name="reply-channel")
data_sender_1, data_receiver_1 = BroadcastChannel[str](name="data-channel-1")
data_sender_2, data_receiver_2 = BroadcastChannel[str](name="data-channel-2")
command_sender, command_receiver = BroadcastChannel[Command](
name="control-channel"
)
reply_sender, reply_receiver = BroadcastChannel[Reply](name="reply-channel")

async with asyncio.TaskGroup() as tasks:
tasks.create_task(
send(
data_channel_1.new_sender(),
command_channel.new_receiver(),
reply_channel.new_sender(),
),
send(data_sender_1, command_receiver, reply_sender.clone()),
name="send-channel-1",
)
tasks.create_task(
send(
data_channel_2.new_sender(),
command_channel.new_receiver(),
reply_channel.new_sender(),
),
send(data_sender_2, command_sender.subscribe(), reply_sender.clone()),
name="send-channel-2",
)
tasks.create_task(
receive(
[data_channel_1.new_receiver(), data_channel_2.new_receiver()],
command_channel.new_receiver(),
reply_channel.new_sender(),
[data_receiver_1, data_receiver_2],
command_sender.subscribe(),
reply_sender,
),
name="receive",
)

control_sender = command_channel.new_sender()
reply_receiver = reply_channel.new_receiver()

# Send a ping command to all tasks and wait for the replies
await control_sender.send(Command.PING)
await command_sender.send(Command.PING)
print(f"main: {await reply_receiver.receive()}")
print(f"main: {await reply_receiver.receive()}")
print(f"main: {await reply_receiver.receive()}")

await asyncio.sleep(5.0)

# Stop senders, after 2 seconds not receiving any data,
# the receiver will stop too
await control_sender.send(Command.STOP_SENDER)
# Stop the senders. This closes the data channels, so the receiver stops too.
await command_sender.send(Command.STOP_SENDER)


asyncio.run(main())
Expand Down
62 changes: 47 additions & 15 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,70 @@

## Summary

This will probably be the last minor release of v1. It deprecates everything that will be removed in v2.0.0.
<!-- Here goes a general summary of what this release is about -->

## Upgrading

- `frequenz-core >= 1.6.0, < 2` is now a runtime dependency. It provides the utilities this project uses to deprecate symbols following the Frequenz deprecations guide.
<!-- Here goes notes on how to upgrade from previous versions, including deprecations and what they should be replaced with -->

- The minimum `typing-extensions` version is now 4.16.0. `frequenz-core` requires 4.13.0, and the new `buffer_size` argument needs the `sentinel` type added in 4.16.0.
- `BroadcastChannel()` and `BroadcastSender.subscribe()` now take everything but the name by keyword: `resend_latest`, `buffer_size` and `warn_on_overflow`. A call that passed any of them by position fails with a `TypeError`. Pass them by keyword, as v1.13.0 recommends.

- `frequenz.channels.Anycast`, deprecated since v1.12.0, will be removed in a future version, not necessarily the next major one. Its deprecation warning now recommends copying its code into your project if you need it, or [opening an issue](https://github.com/frequenz-floss/frequenz-channels-python/issues) if you think it should stay in the library.
- The deprecated `limit` argument of `BroadcastChannel()` and `BroadcastSender.subscribe()` is removed. Use `buffer_size` instead, which exists since v1.13.0, so you can migrate before upgrading. `Broadcast.new_receiver()` still takes `limit`.

- `watchfiles` versions up to 1.3 are now allowed.
- The deprecated `frequenz.channels.timer.Timer` is removed. Use `MonotonicTimer` instead, which exists since v1.13.0, so you can migrate before upgrading. The name `Timer` will be used for a wall clock timer in a later version.

- `frequenz.channels.experimental.GroupingLatestValueCache` and its type variables `DefaultT`, `HashableT` and `ValueT_co`, and `frequenz.channels.experimental.RelaySender` are deprecated and will be removed in v2.0.0. There is no replacement in this library: copy their code into your project if you need them, or [open an issue](https://github.com/frequenz-floss/frequenz-channels-python/issues) if you think they should stay.
- `Anycast` is removed. There is no replacement: copy its code into your project if you still need it.

- `frequenz.channels.experimental.NopReceiver` and `frequenz.channels.experimental.WithPrevious` are deprecated. Import them from `frequenz.channels` instead; they are the same objects, so only the import changes. The old names will be removed in v2.0.0.
- `frequenz.channels.experimental.GroupingLatestValueCache` is removed, together with its type variables `DefaultT`, `HashableT` and `ValueT_co`. Copy its code into your project if you still need it, or open an issue if you think it should stay in the library.

- The deprecation warning of `frequenz.channels.experimental.OptionalReceiver` now points at `frequenz.channels.NopReceiver`. Use the receiver itself, or a `NopReceiver` when there is none.
- `frequenz.channels.experimental.RelaySender` is removed. Copy its code into your project if you still need it, or open an issue if you think it should stay in the library.

- `frequenz.channels.timer.Timer` was renamed to `MonotonicTimer`, to make clear it uses the event loop's monotonic clock and to free the name `Timer` for a wall clock timer in a later version. `Timer` still works as a deprecated alias and will be removed in v2.0.0. The change is mechanical: replace `Timer` with `MonotonicTimer` in imports and uses.
- `frequenz.channels.experimental.OptionalReceiver` is removed. Use the receiver itself, or `frequenz.channels.NopReceiver` when there is none. `NopReceiver` exists since v1.13.0, so you can migrate before upgrading.

- The `limit` argument of `BroadcastChannel()` and `BroadcastSender.subscribe()` is deprecated and will be removed in v2.0.0. Use the new `buffer_size` argument instead. It does the same, but its name says what is limited: the number of messages each receiver can hold. `buffer_size` is keyword-only. A call that passes both `limit` and `buffer_size` raises a `TypeError`. Passing `warn_on_overflow` by position is deprecated too, because the new form only accepts it by keyword: pass it by keyword. v2.0.0 will also make `resend_latest` keyword-only, so pass it by keyword too. `Broadcast` and `Anycast` are not changed.
- `WithPrevious` and `NopReceiver` are no longer available from `frequenz.channels.experimental`. Import them from `frequenz.channels`, where they are available since v1.13.0, so you can migrate before upgrading.

- The deprecated `async close()` method of `Broadcast` is removed. Use `aclose()` instead, which exists since v1.8.0, so you can migrate before upgrading. It also works with `contextlib.aclosing()`.

- The `repr()` of a `BroadcastReceiver` shows its `buffer_size` instead of `limit`.

- A `BroadcastChannel`, or a `Broadcast` created with `auto_close=True`, no longer closes when a message is sent while it has no receivers. `send()` now drops the message and raises a `SenderHasNoReceiversError`, a `SenderError` without a cause. The channel stays open: `subscribe()` keeps working, and sending works again once a new receiver is attached.

- Code that waited for such a channel to close after a send without receivers, for example by checking for a `ChannelClosedError` cause or for `is_closed`, should catch `SenderHasNoReceiversError` instead. It can then stop sending, or close all of the channel's senders to close the channel.

- An experimental `Pipe` now closes its sender when its receiver stops. Before, it left the sender open.

- When sending fails with a `SenderError`, for example because the sender's `BroadcastChannel` has no receivers, a `Pipe` now closes both its sender and its receiver, and logs the error: a `SenderHasNoReceiversError` as a warning, and any other error with its traceback. Before, the pipe stopped but left both open.

- `Pipe.stop()` still leaves a pipe's sender and receiver open. So does any exception while receiving, other than the receiver stopping, and any exception from sending that isn't a `SenderError`.

- To keep a `Pipe` running after a `SenderError`, pass `close_pipe_on_send_error=False`. It then drops the message, keeps sending the messages that follow, and logs a `SenderHasNoReceiversError` at debug level.

- Give a `Pipe` a clone of its sender if you still need to send after the pipe stops on its own.

- `BroadcastSender.clone()` now raises a `SenderClosedError` if the sender is closed, and a `SenderError` if the channel is closed, like `send()` and `subscribe()` do. Clone a sender before closing it, or catch `SenderError`.

- `BroadcastSender.subscribe()` now uses the `buffer_size` and `warn_on_overflow` values the `BroadcastChannel` was created with as defaults, instead of `50` and `True`. Pass `buffer_size=50, warn_on_overflow=True` to keep the old values.

- Call `BroadcastReceiver.close()` from the thread that runs the event loop of the channel's receivers. From another thread, tasks waiting on any receiver of the channel might not wake up. If asyncio debug mode is on and a task is waiting, `close()` also raises a `RuntimeError`, and the waiting tasks stay stuck. Use `loop.call_soon_threadsafe(receiver.close)` instead.

- `Receiver.close()` is now an abstract method, so a custom `Receiver` subclass that doesn't implement it can't be instantiated anymore. Before, calling it raised a `NotImplementedError`. All the receivers in this library implement it already. Add a `close()` method to your own receivers; the same implementation works on v1, so your code can support both major versions.

## New Features

- `NopReceiver` and `WithPrevious` are now stable, and available from `frequenz.channels`.
<!-- Here goes the main new features and examples or instructions on how to use them -->

- `TransientSenderError` is a new `SenderError` for errors after which the sender and its channel can still be used: only the message that failed wasn't sent. Catch it before `SenderError` to tell it apart from the errors that make a sender unusable.

- `SenderHasNoReceiversError` is a new `TransientSenderError`. An open sender of a `BroadcastChannel`, or of a `Broadcast` created with `auto_close=True`, raises it from `send()` while the channel is open but has no receivers. The message is dropped, but with `resend_latest=True`, new receivers still get it if it is the last message sent and it isn't `None`. The channel stays open, so sending works again once a receiver subscribes.

- `BroadcastSender.has_receivers` and `BroadcastReceiver.has_senders` tell whether the other side of a broadcast channel is still there, so producers can stop producing messages that nobody would receive. A sender that was garbage-collected without being closed still counts.

## Bug Fixes

- `BroadcastChannel()` no longer emits the deprecation warning of `Broadcast`, which it uses internally. You were getting a warning about a class you never used.
<!-- Here goes notable bug fixes that are worth a special mention or explanation -->

- `OneshotReceiver.close()` no longer raises `NotImplementedError`. It now works like in the other receivers: a message sent before closing can still be received, and after that receiving raises `ReceiverStoppedError`. The sender can still send, but its message is discarded.
- Closing a broadcast receiver now wakes up tasks waiting for messages on it, instead of leaving them blocked until the next message arrived or the channel was closed.

- Creating a broadcast receiver with a `buffer_size` (or `limit`) of 0 now raises a `ValueError`, from `BroadcastChannel()` and `BroadcastSender.subscribe()`. Before, the first message sent to it raised an `IndexError`, after other receivers might have got the message already.
- Closing a broadcast receiver now drops the messages left in its buffer, so it is no longer reported as ready. So `select()` no longer reports it as stopped over and over, and no longer fails with a `SelectError` for a `filter()` on it.

- Docstrings now use absolute cross-references to public paths instead of relative ones. Relative cross-references are expanded from the module where an object is defined since mkdocstrings-python 2.0.9, which broke some links, and projects rendering docstrings inherited from this library needed `relative_crossrefs: true` for them to work at all. See [frequenz-repo-config-python#655](https://github.com/frequenz-floss/frequenz-repo-config-python/issues/655) for details.
- Sending to or closing a broadcast channel no longer raises `RuntimeError: Event loop is closed` when a receiver was left waiting on an event loop that has since been closed.
127 changes: 0 additions & 127 deletions benchmarks/benchmark_anycast.py

This file was deleted.

2 changes: 1 addition & 1 deletion docs/user-guide/channels/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ various factors:
Because these questions can have many answers, there are different types of
channels. Frequenz Channels offers a few of them:

* [Anycast](anycast.md)
* [Broadcast](broadcast.md)
* [Oneshot](oneshot.md)

More might be added in the future, and you can also create your own.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Anycast
# Oneshot

::: frequenz.channels._anycast.Anycast
::: frequenz.channels._oneshot.OneshotChannel
options:
inherited_members: []
members: []
Expand Down
Loading