Sending¤
Sender interface and related exceptions.
Senders¤
Messages are sent to a channel through
Sender objects. Senders are
usually created by calling channel.new_sender()
, and are a very simple abstraction
that only provides a single send()
method:
from frequenz.channels import Anycast
channel = Anycast[int](name="test-channel")
sender = channel.new_sender()
await sender.send("Hello, world!")
Even when send()
is an asynchronous method, some
channels may implement it as a synchronously. For example, buffered channels that
drop messages when the buffer is full could guarantee that
send()
never blocks. However, please keep in mind
that the asyncio event loop could give control to another task at any time,
effectively making the send()
method blocking.
Error Handling¤
Tip
For more information about handling errors, please refer to the Error Handling section of the user guide.
If there is any failure sending a message, a SenderError exception is raised.
from frequenz.channels import Anycast
channel = Anycast[int](name="test-channel")
sender = channel.new_sender()
try:
await sender.send("Hello, world!")
except SenderError as error:
print(f"Error sending message: {error}")