| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
Sorry, something went wrong.
Co-authored-by: Guido van Rossum <guido@python.org>
Co-authored-by: Zac Hatfield-Dodds <zac.hatfield.dodds@gmail.com>
pythongh-120924: Simplify consumers by adding `*.Queue.iter()` and `*.Queue.iter_nowait()`
|
Sorry, I didn't intend to request a review. I merged the pull requests to leave it at just 2. |
Sorry, something went wrong.
|
I’m sorry, I am currently on vacation and dealing with health issues. I will not be a available to review this. |
Sorry, something went wrong.
…9-35-14.gh-issue-119154.7CXFaX.rst
|
Note, I (with Erlend Aasland concurring) marked #120503 as rejected 2 weeks ago. Rather than accept the result from the Queue module maintainer, the OP immediately opened this new PR with substantially the same proposal and code (plus other additions). No mention was made of the prior rejection or reasons for it. This seems deceptive and inappropriate. |
Sorry, something went wrong.
|
I first asked whether this would be a better API: #120503 (comment). And clarified how Empty exceptions would be handled: #120503 (comment). You haven't given your opinion yet. I'll attempt tot address the previous rejection. |
Sorry, something went wrong.
If you want, we can delay this change until Python 3.15.
Is it clearer with q.iter() and q.iter_nowait()? That's should be as clear as get() and get_nowait().
q.iter() provides the same options now.
We haven't done that with Shutdown either. This simply makes it more powerful.
According to my GitHub search, only a minority of 24.5% are "simple" consumers:4
"performance is not the only golden rule for CPython. Code readability is critical for maintenance"5
q.iter() makes this a bit more explicit. I think which exceptions are caught should make sense.
An error is raised if it's the first element. "This ensures the queue is not empty (without risking race conditions). That seems like the only sensible behaviour as queues aren't infinite (why would you otherwise want to iterate over them without waiting).7 def iter(self, block=True, timeout=None):
try:
yield self.get(block=block, timeout=timeout)
except ShutDown:
return
try:
while True:
yield self.get(block=block, timeout=timeout)
except (Empty, ShutDown):
return
You can do that after the for loop:8 for ... in q.iter():
# do something
cleanup(q)
In that case, create a subclass.8
It's safer to call q.task_done() explicitly, see the discussion here: #119154.9
I think that in most cases a subclass won't be necessary. Footnotes
|
Sorry, something went wrong.
|
We discussed this at the sprint today and agreed this (or any variation of it) should not be done. Multiple reasons were given. The most important was that this is only one of the multitude of reasonable ways to use existing queue API: the new shutdown exception, previously existing custom exceptions, waiting for timeouts, multiple producers, multiple consumers, having a second queue for two-way communication, etc. Another reason was that a senior concurrency developer had become averse to trend widening existing APIs to support the various dunder method protocols (iterator, context manager etc). Other devs agreed that a for-loop front-end would hide (make implicit) details that we really want to be explicit for code review (i.e. when blocking happens and what exception will be caught). This senior concurrency dev suggested that I write a small edit to the docs showing a user could easily write a wrapper like this. I'll do that in a separate PR. |
Sorry, something went wrong.
|
Does this decision apply to #120491 as well? In that case both the issue and PR should be closed. |
Sorry, something went wrong.
| Back | FazBrowse Home | New Git URL |
Purpose
Currently consuming items from a queue can be a bit more complex than necessary. You need to
By adding *.Queue.iter() and *.Queue.iter_nowait(), this becomes a bit easier, you only need to call queue.shutdown().
Overview of changes
asyncio
queue.py
Examples
asyncio
Without iteration
With iteration
Output
queue as final step
Without iteration
With iteration
Output
queue not as final step
Without iteration
With iteration
Output
📚 Documentation preview 📚: library/queue.html library/asyncio-queue.html