@@ -81,17 +81,10 @@ class AsyncChannel(AsyncIterable[T]):
8181 """
8282
8383 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 ,
8985 ):
9086 self ._queue : asyncio .Queue [Union [T , object ]] = asyncio .Queue (buffer_limit )
9187 self ._closed = False
92- self ._sending_task = (
93- asyncio .ensure_future (self .send_from (source , close )) if source else None
94- )
9588 self ._waiting_recievers : int = 0
9689 # Track whether flush has been invoked so it can only happen once
9790 self ._flushed = False
@@ -100,13 +93,14 @@ def __aiter__(self) -> AsyncIterator[T]:
10093 return self
10194
10295 async def __anext__ (self ) -> T :
103- if self .done :
96+ if self .done () :
10497 raise StopAsyncIteration
10598 self ._waiting_recievers += 1
10699 try :
107100 result = await self ._queue .get ()
108101 if result is self .__flush :
109102 raise StopAsyncIteration
103+ return result
110104 finally :
111105 self ._waiting_recievers -= 1
112106 self ._queue .task_done ()
@@ -131,7 +125,7 @@ def done(self) -> bool:
131125
132126 async def send_from (
133127 self , source : Union [Iterable [T ], AsyncIterable [T ]], close : bool = False
134- ):
128+ ) -> "AsyncChannel[T]" :
135129 """
136130 Iterates the given [Async]Iterable and sends all the resulting items.
137131 If close is set to True then subsequent send calls will be rejected with a
@@ -151,24 +145,26 @@ async def send_from(
151145 await self ._queue .put (item )
152146 if close :
153147 # Complete the closing process
154- await self .close ()
148+ self .close ()
149+ return self
155150
156- async def send (self , item : T ):
151+ async def send (self , item : T ) -> "AsyncChannel[T]" :
157152 """
158153 Send a single item over this channel.
159154 :param item: The item to send
160155 """
161156 if self ._closed :
162157 raise ChannelClosed ("Cannot send through a closed channel" )
163158 await self ._queue .put (item )
159+ return self
164160
165161 async def recieve (self ) -> Optional [T ]:
166162 """
167163 Returns the next item from this channel when it becomes available,
168164 or None if the channel is closed before another item is sent.
169165 :return: An item from the channel
170166 """
171- if self .done :
167+ if self .done () :
172168 raise ChannelDone ("Cannot recieve from a closed channel" )
173169 self ._waiting_recievers += 1
174170 try :
@@ -184,8 +180,6 @@ def close(self):
184180 """
185181 Close this channel to new items
186182 """
187- if self ._sending_task is not None :
188- self ._sending_task .cancel ()
189183 self ._closed = True
190184 asyncio .ensure_future (self ._flush_queue ())
191185
0 commit comments