1
1
"""Pipeline sections with age- and volume-based buffers."""
2
2
from collections import deque
3
3
import math
4
- from typing import Any , AsyncIterable , Callable , Optional , Sequence
4
+ from typing import Any , Callable , Optional , Sequence
5
5
6
6
import trio
7
- from async_generator import aclosing
8
7
9
8
from ..environments import TrioSection
9
+ from .._types import AsyncIterableWithAcloseableIterator
10
+ from .._utils import aclosing
10
11
11
12
class Window (TrioSection ):
12
13
"""Window buffer with size and age limits.
@@ -26,13 +27,13 @@ class Window(TrioSection):
26
27
:param max_size: The maximum buffer size.
27
28
:type max_size: int
28
29
:param source: Input when used as first section.
29
- :type source: Optional[AsyncIterable [Any]]
30
+ :type source: Optional[AsyncIterableWithAcloseableIterator [Any]]
30
31
:param max_age: Maximum item age in seconds. (default: unlimited)
31
32
:type max_age: float
32
33
:param min_size: Minimum amount of items in the buffer to trigger an output.
33
34
:type min_size: int
34
35
"""
35
- def __init__ (self , max_size : int , source : Optional [AsyncIterable [Any ]] = None , * ,
36
+ def __init__ (self , max_size : int , source : Optional [AsyncIterableWithAcloseableIterator [Any ]] = None , * ,
36
37
max_age : float = math .inf ,
37
38
min_size : int = 1 ):
38
39
super ().__init__ ()
@@ -51,7 +52,7 @@ async def refine(self, input, output):
51
52
52
53
buf = deque ()
53
54
54
- async with aclosing (source ) as aiter :
55
+ async with aclosing (source . __aiter__ () ) as aiter :
55
56
async for item in aiter :
56
57
now = trio .current_time ()
57
58
buf .append ((item , now ))
@@ -80,7 +81,7 @@ class Group(TrioSection):
80
81
:param interval: Time in seconds from when an item arrives until the buffer is sent.
81
82
:type interval: float
82
83
:param source: Input when used as first section.
83
- :type source: Optional[AsyncIterable [Any]]
84
+ :type source: Optional[AsyncIterableWithAcloseableIterator [Any]]
84
85
:param max_size: Maximum number of items in buffer, which when reached, will cause the buffer
85
86
to be sent.
86
87
:type max_size: int
@@ -89,7 +90,7 @@ class Group(TrioSection):
89
90
:param reducer: Optional reducer function used to transform the buffer to a single value.
90
91
:type reducer: Optional[Callable[[Sequence[Any]], Any]]
91
92
"""
92
- def __init__ (self , interval : float , source : Optional [AsyncIterable [Any ]] = None , * ,
93
+ def __init__ (self , interval : float , source : Optional [AsyncIterableWithAcloseableIterator [Any ]] = None , * ,
93
94
max_size : Optional [int ] = None ,
94
95
mapper : Optional [Callable [[Any ], Any ]] = None ,
95
96
reducer : Optional [Callable [[Sequence [Any ]], Any ]] = None ):
@@ -111,7 +112,7 @@ async def refine(self, input, output):
111
112
112
113
send_channel , receive_channel = trio .open_memory_channel (0 )
113
114
async def pull_task ():
114
- async with send_channel , aclosing (source ) as aiter :
115
+ async with send_channel , aclosing (source . __aiter__ () ) as aiter :
115
116
async for item in aiter :
116
117
await send_channel .send (item )
117
118
nursery .start_soon (pull_task )
@@ -152,9 +153,9 @@ class Delay(TrioSection):
152
153
:param interval: Number of seconds that each item is delayed.
153
154
:type interval: float
154
155
:param source: Input when used as first section.
155
- :type source: Optional[AsyncIterable [Any]]
156
+ :type source: Optional[AsyncIterableWithAcloseableIterator [Any]]
156
157
"""
157
- def __init__ (self , interval : float , source : Optional [AsyncIterable [Any ]] = None ):
158
+ def __init__ (self , interval : float , source : Optional [AsyncIterableWithAcloseableIterator [Any ]] = None ):
158
159
super ().__init__ ()
159
160
self .source = source
160
161
self .interval = interval
@@ -169,7 +170,7 @@ async def refine(self, input, output):
169
170
buffer_input_channel , buffer_output_channel = trio .open_memory_channel (math .inf )
170
171
171
172
async def pull_task ():
172
- async with buffer_input_channel , aclosing (source ) as aiter :
173
+ async with buffer_input_channel , aclosing (source . __aiter__ () ) as aiter :
173
174
async for item in aiter :
174
175
await buffer_input_channel .send ((item , trio .current_time () + self .interval ))
175
176
0 commit comments