11# License: MIT
22# Copyright © 2022 Frequenz Energy-as-a-Service GmbH
33
4- """Select the first among multiple AsyncIterators .
4+ """Select the first among multiple Receivers .
55
6- Expects AsyncIterator class to raise `StopAsyncIteration`
6+ Expects Receiver class to raise `StopAsyncIteration`
77exception once no more messages are expected or the channel
88is closed in case of `Receiver` class.
99"""
1010
1111import asyncio
1212import logging
1313from dataclasses import dataclass
14- from typing import Any , AsyncIterator , Dict , List , Optional , Set , TypeVar
14+ from typing import Any , Dict , List , Optional , Set , TypeVar
15+
16+ from frequenz .channels .base_classes import Receiver
1517
1618logger = logging .Logger (__name__ )
1719T = TypeVar ("T" )
@@ -29,17 +31,17 @@ class _Selected:
2931
3032
3133class Select :
32- """Select the next available message from a group of AsyncIterators .
34+ """Select the next available message from a group of Receivers .
3335
34- If `Select` was created with more `AsyncIterator ` than what are read in
36+ If `Select` was created with more `Receiver ` than what are read in
3537 the if-chain after each call to [ready()][frequenz.channels.Select.ready],
36- messages coming in the additional async iterators are dropped, and
38+ messages coming in the additional receivers are dropped, and
3739 a warning message is logged.
3840
39- [Receiver][frequenz.channels.Receiver]s also function as `AsyncIterator `.
41+ [Receiver][frequenz.channels.Receiver]s also function as `Receiver `.
4042
4143 Example:
42- For example, if there are two async iterators that you want to
44+ For example, if there are two receivers that you want to
4345 simultaneously wait on, this can be done with:
4446
4547 ```python
@@ -58,11 +60,11 @@ class Select:
5860 ```
5961 """
6062
61- def __init__ (self , ** kwargs : AsyncIterator [Any ]) -> None :
63+ def __init__ (self , ** kwargs : Receiver [Any ]) -> None :
6264 """Create a `Select` instance.
6365
6466 Args:
65- **kwargs: sequence of async iterators
67+ **kwargs: sequence of receivers
6668 """
6769 self ._receivers = kwargs
6870 self ._pending : Set [asyncio .Task [Any ]] = set ()
@@ -84,10 +86,10 @@ def __del__(self) -> None:
8486 task .cancel ()
8587
8688 async def ready (self ) -> bool :
87- """Wait until there is a message in any of the async iterators .
89+ """Wait until there is a message in any of the receivers .
8890
8991 Returns `True` if there is a message available, and `False` if all
90- async iterators have closed.
92+ receivers have closed.
9193
9294 Returns:
9395 Whether there are further messages or not.
@@ -102,7 +104,7 @@ async def ready(self) -> bool:
102104 self ._ready_count = 0
103105 self ._prev_ready_count = 0
104106 logger .warning (
105- "Select.ready() dropped data from async iterator (s): %s, "
107+ "Select.ready() dropped data from receiver (s): %s, "
106108 "because no messages have been fetched since the last call to ready()." ,
107109 dropped_names ,
108110 )
@@ -127,7 +129,7 @@ async def ready(self) -> bool:
127129 result = item .result ()
128130 self ._ready_count += 1
129131 self ._result [name ] = _Selected (result )
130- # if channel or AsyncIterator is closed
132+ # if channel or Receiver is closed
131133 # don't add a task for it again.
132134 if result is None :
133135 continue
@@ -138,13 +140,13 @@ async def ready(self) -> bool:
138140 return True
139141
140142 def __getattr__ (self , name : str ) -> Optional [Any ]:
141- """Return the latest unread message from a `AsyncIterator `, if available.
143+ """Return the latest unread message from a `Receiver `, if available.
142144
143145 Args:
144146 name: Name of the channel.
145147
146148 Returns:
147- Latest unread message for the specified `AsyncIterator `, or `None`.
149+ Latest unread message for the specified `Receiver `, or `None`.
148150
149151 Raises:
150152 KeyError: when the name was not specified when creating the
0 commit comments