@@ -81,17 +81,10 @@ class AsyncChannel(AsyncIterable[T]):
81
81
"""
82
82
83
83
def __init__ (
84
- self ,
85
- source : Union [Iterable [T ], AsyncIterable [T ]] = tuple (),
86
- * ,
87
- buffer_limit : int = 0 ,
88
- close : bool = False ,
84
+ self , * , buffer_limit : int = 0 , close : bool = False ,
89
85
):
90
86
self ._queue : asyncio .Queue [Union [T , object ]] = asyncio .Queue (buffer_limit )
91
87
self ._closed = False
92
- self ._sending_task = (
93
- asyncio .ensure_future (self .send_from (source , close )) if source else None
94
- )
95
88
self ._waiting_recievers : int = 0
96
89
# Track whether flush has been invoked so it can only happen once
97
90
self ._flushed = False
@@ -100,13 +93,14 @@ def __aiter__(self) -> AsyncIterator[T]:
100
93
return self
101
94
102
95
async def __anext__ (self ) -> T :
103
- if self .done :
96
+ if self .done () :
104
97
raise StopAsyncIteration
105
98
self ._waiting_recievers += 1
106
99
try :
107
100
result = await self ._queue .get ()
108
101
if result is self .__flush :
109
102
raise StopAsyncIteration
103
+ return result
110
104
finally :
111
105
self ._waiting_recievers -= 1
112
106
self ._queue .task_done ()
@@ -131,7 +125,7 @@ def done(self) -> bool:
131
125
132
126
async def send_from (
133
127
self , source : Union [Iterable [T ], AsyncIterable [T ]], close : bool = False
134
- ):
128
+ ) -> "AsyncChannel[T]" :
135
129
"""
136
130
Iterates the given [Async]Iterable and sends all the resulting items.
137
131
If close is set to True then subsequent send calls will be rejected with a
@@ -151,24 +145,26 @@ async def send_from(
151
145
await self ._queue .put (item )
152
146
if close :
153
147
# Complete the closing process
154
- await self .close ()
148
+ self .close ()
149
+ return self
155
150
156
- async def send (self , item : T ):
151
+ async def send (self , item : T ) -> "AsyncChannel[T]" :
157
152
"""
158
153
Send a single item over this channel.
159
154
:param item: The item to send
160
155
"""
161
156
if self ._closed :
162
157
raise ChannelClosed ("Cannot send through a closed channel" )
163
158
await self ._queue .put (item )
159
+ return self
164
160
165
161
async def recieve (self ) -> Optional [T ]:
166
162
"""
167
163
Returns the next item from this channel when it becomes available,
168
164
or None if the channel is closed before another item is sent.
169
165
:return: An item from the channel
170
166
"""
171
- if self .done :
167
+ if self .done () :
172
168
raise ChannelDone ("Cannot recieve from a closed channel" )
173
169
self ._waiting_recievers += 1
174
170
try :
@@ -184,8 +180,6 @@ def close(self):
184
180
"""
185
181
Close this channel to new items
186
182
"""
187
- if self ._sending_task is not None :
188
- self ._sending_task .cancel ()
189
183
self ._closed = True
190
184
asyncio .ensure_future (self ._flush_queue ())
191
185
0 commit comments