@@ -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
@@ -132,7 +125,7 @@ def done(self) -> bool:
132
125
133
126
async def send_from (
134
127
self , source : Union [Iterable [T ], AsyncIterable [T ]], close : bool = False
135
- ):
128
+ ) -> "AsyncChannel[T]" :
136
129
"""
137
130
Iterates the given [Async]Iterable and sends all the resulting items.
138
131
If close is set to True then subsequent send calls will be rejected with a
@@ -153,15 +146,17 @@ async def send_from(
153
146
if close :
154
147
# Complete the closing process
155
148
self .close ()
149
+ return self
156
150
157
- async def send (self , item : T ):
151
+ async def send (self , item : T ) -> "AsyncChannel[T]" :
158
152
"""
159
153
Send a single item over this channel.
160
154
:param item: The item to send
161
155
"""
162
156
if self ._closed :
163
157
raise ChannelClosed ("Cannot send through a closed channel" )
164
158
await self ._queue .put (item )
159
+ return self
165
160
166
161
async def recieve (self ) -> Optional [T ]:
167
162
"""
@@ -185,8 +180,6 @@ def close(self):
185
180
"""
186
181
Close this channel to new items
187
182
"""
188
- if self ._sending_task is not None :
189
- self ._sending_task .cancel ()
190
183
self ._closed = True
191
184
asyncio .ensure_future (self ._flush_queue ())
192
185
0 commit comments