Skip to content

Commit eba86d9

Browse files
committed
Improve the _sender module description
Give a short introduction about how `Sender`s are created and used, including how to handle errors. This module is not publicly available, so users won't be able to access the information in IDEs for example, but it will be rendered as part of the User Guide. Signed-off-by: Leandro Lucarella <[email protected]>
1 parent 9e2e739 commit eba86d9

File tree

1 file changed

+48
-2
lines changed

1 file changed

+48
-2
lines changed

src/frequenz/channels/_sender.py

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,53 @@
11
# License: MIT
22
# Copyright © 2022 Frequenz Energy-as-a-Service GmbH
33

4-
"""Channel sender and associated exceptions."""
4+
"""Sender interface and related exceptions.
5+
6+
# Senders
7+
8+
Messages are sent to a [channel](/user-guide/channels) through
9+
[Sender][frequenz.channels.Sender] objects. [Senders][frequenz.channels.Sender] are
10+
usually created by calling `channel.new_sender()`, and are a very simple abstraction
11+
that only provides a single [`send()`][frequenz.channels.Sender.send] method:
12+
13+
```python
14+
from frequenz.channels import Anycast
15+
16+
channel = Anycast[int](name="test-channel")
17+
sender = channel.new_sender()
18+
19+
await sender.send("Hello, world!")
20+
```
21+
22+
Even when [`send()`][frequenz.channels.Sender.send] is an asynchronous method, some
23+
channels may implement it as a synchronously. For example, buffered channels that
24+
drop messages when the buffer is full could guarantee that
25+
[`send()`][frequenz.channels.Sender.send] never blocks. However, please keep in mind
26+
that the [asyncio][] event loop could give control to another task at any time,
27+
effectively making the [`send()`][frequenz.channels.Sender.send] method blocking.
28+
29+
# Error Handling
30+
31+
!!! Tip inline end
32+
33+
For more information about handling errors, please refer to the
34+
[Error Handling](/user-guide/error-handling/) section of the user guide.
35+
36+
If there is any failure sending a message,
37+
a [SenderError][frequenz.channels.SenderError] exception is raised.
38+
39+
```python
40+
from frequenz.channels import Anycast
41+
42+
channel = Anycast[int](name="test-channel")
43+
sender = channel.new_sender()
44+
45+
try:
46+
await sender.send("Hello, world!")
47+
except SenderError as error:
48+
print(f"Error sending message: {error}")
49+
```
50+
"""
551

652
from abc import ABC, abstractmethod
753
from typing import Generic, TypeVar
@@ -12,7 +58,7 @@
1258

1359

1460
class Sender(ABC, Generic[_T]):
15-
"""A channel Sender."""
61+
"""An entity that sends values to a *channel*."""
1662

1763
@abstractmethod
1864
async def send(self, msg: _T) -> None:

0 commit comments

Comments
 (0)