New submission from Vladimir Filipović <hemf...@gmail.com>:

Code using threading.Queue often needs to coordinate a "work is finished as far 
as far as I care" state between the producing and consuming side.

When going from the producer to the consumer ("No more items after this, so 
don't bother waiting"), this is usually implemented with sentinel objects, 
which is at best needlessly verbose and at worst tricky to get right (as with 
multiple consumers, or communicating a non-trivial sentinel object).
When going the other direction ("I'm not interested in consuming any more, so 
you can stop putting them on the queue"), or when a third component needs to 
notify both sides ("You two start wrapping up, but don't drop any in-flight 
items") there isn't even a clear usual solution.

Adding a close() method to the Queue (with accompanying exception treatment 
etc.) would solve all of this in a very clean way. It would not change anything 
for code that doesn't want to use it. It would simplify a lot of everyday uses 
of Queue. Many simple producers could reduce their coordination code to a `with 
closing(queue)` idiom. A further __iter__() method would enable many simple 
consumers to safely cut all their coordination boilerplate down to just `for 
item in queue`.

I've got a sample implementation ready for Queue and its children, and I'm 
about to submit it as a PR.

I'd be happy to contribute an equivalent (within limits of its API promises) 
implementation for SimpleQueue, but I don't have it written, and I'm unsure if 
I should make a separate issue for that.

----------
components: Library (Lib)
messages: 328181
nosy: hemflit
priority: normal
severity: normal
status: open
title: Add closing and iteration to threading.Queue
type: enhancement
versions: Python 3.8

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue35034>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to