Some thoughts on AsyncBufferedReader #1720
grant-allan-ctct
started this conversation in
Ideas
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I'm pretty new to this library and a learner when it comes to Python and especially asyncio, so there may be not much virtue in these ideas, but y'all seem like a friendly bunch so I'll put pen to paper anyway and see what y'all think.
Background info:
The
AsyncBufferedReaderholds aself.bufferthat's anasyncio.Queue, as can be see here.Such a queue has an internal counter
_unfinished_tasksthat's incremented each time thatput_nowaitis called. Is also has an event-lock thingy called_finishedwhich has itsset()method called at queue construction and itsclear()method whenput_nowaitis called. Looking at the other places where_unfinished_tasksand_finishedused, we can see how theasyncio.Queueachieves the functionality that's documented for itsjoin()method, viz. waiting until all queued items have been "processed" and then closing shop (see here).Wonderings:
Allow
async forloops to end?The implementation of
__anext__on theAsyncBufferedReaderclass is written in such a way that iterating over the collection will never end, and further, the iterator seems willing to wait forever for the next message to arrive. To clarify, I'll mention the following example code snippet is taken from the docstring forAyncBufferedReaderhere:If we write something like that in our code though, what happens that any lines of code that come afterwards are effectively unreachable. I realize that it's possible that this is exactly what we want, but there could be some other options:
_unfinished_tasksthat the queue is tracking, and have__anext__raise aStopAsyncIterationif it gets to 0. This will mark theasync forloop as being complete, and program execution can then continue. In order to do this, we would need to make a call toself.buffer.task_done()every time we successfully removed an item from the queue usingself.buffer.get(), as described here.await asyncio.wait_for(self.buffer.get(), timeout)inside__anext__and check for occurrence of a timeout; if one occurs, then either:raise StopAsyncIteration, orNoneand allow the universe to decide how to respond, e.g. maybe, or maybe not,breakout of the loopProvide an optional timeout for
get_message?Speaking of timeouts, it might be nice for the
AsyncBufferedReaderto have a common default timeout that it can share between the__anext__implementation and theget_messageimplementation (here). And, ifget_messagewere given an optionaltimeoutargument then__anext__could just callreturn await get_message()(depending on outcome of prior questions).Remove a smidge of DRY violation ?
Actually, seems like having
__anext__just callreturn await get_message()could happen already 😎 .Beta Was this translation helpful? Give feedback.
All reactions