bpo-30966: Add multiprocessing.SimpleQueue.close()#2760
bpo-30966: Add multiprocessing.SimpleQueue.close()#2760vstinner wants to merge 4 commits intopython:masterfrom vstinner:simplequeue
Conversation
Lib/multiprocessing/queues.py
Outdated
There was a problem hiding this comment.
You shouldn't need that: the reader and writer already have a __del__.
There was a problem hiding this comment.
Hum, maybe I gone too far. This change is part on a larger plan to add ResourceWarning everywhere.
There was a problem hiding this comment.
I removed del(), sorry this change was not directly related to this PR. It can be added later if needed (for ResourceWarning).
Lib/test/_test_multiprocessing.py
Outdated
There was a problem hiding this comment.
Should this also need explicit testing _reader and _writer's .closed to be True?
There was a problem hiding this comment.
I dislike testing private attributes in unit tests. Should I add a second @cpython_only unit test testing private attributes?
There was a problem hiding this comment.
I added functional tests instead.
Lib/test/_test_multiprocessing.py
Outdated
There was a problem hiding this comment.
What happens if I call get() or put() on a closed queue?
There was a problem hiding this comment.
What happens if I call get() or put() on a closed queue?
No idea :-) Which behaviour do you expect/want?
There was a problem hiding this comment.
Usually, it would raise a ValueError.
Lib/multiprocessing/queues.py
Outdated
There was a problem hiding this comment.
How much overhead adds this?
There was a problem hiding this comment.
No idea. Does it matter? Correctness beats performance, no?
There was a problem hiding this comment.
The performance matters. What bug fixed by adding this call?
There was a problem hiding this comment.
There's so much going on here (I/O, pickling) that I don't think a trivial method call is going to matter.
There was a problem hiding this comment.
There is a race condition here. AttributeError is raised if the queue is closed after calling _check_closed(). Maybe use locking in close() and guard the check with a lock?
There was a problem hiding this comment.
SimpleQueue is supposed to be thread-safe? (I don't know the answer, it's a real question!) If we start to use the read lock, we also may have to use the write lock??
There was a problem hiding this comment.
Otherwise why it use the locks?
Doc/whatsnew/3.7.rst
Outdated
There was a problem hiding this comment.
I'm not sure, but seems there are errors in English here.
There was a problem hiding this comment.
yup, same typo as below: should be "to explicitly close the..."
|
I rewrote close() to close the writer even if closing the reader fails. It also clears _reader and _writer references to help the garbage collector. I chose to remove the _poll attribute to simplify the implementation. But I don't know the rationale of the _poll() method. Was it added to add a private _poll() method? Or is it to prevent issues with the garbage collector on Python finalization? SimpleQueue._poll attribute was added by the commit bdb1cf1 and http://bugs.python.org/issue12328: So @pitrou: Is it safe to remove _poll? Or should I keep it? If we keep it, I would prefer to explain why we need it. |
There was a problem hiding this comment.
should be "to explicitly close the..."
* Add a new close() method to multiprocessing.SimpleQueue to explicitly close the queue. * Operation on a closed queue raises a ValueError. * Remove the SimpleQueue._poll attribute: use self._reader.poll()
|
Ok, I think the current diff has gone too far :-) If raising ValueError cannot be provided without adding a lot of locking and complication in the implementation, then I'll happily lose the ValueError requirement so that the implementation remains simple and lightweight (it's called "SimpleQueue" for a reason :-)). |
The complicated code comes from my will of setting attributes to None. Maybe if we only call .close() without setting attributes to None, it would simplify the code, no? What do you think? |
I don't know, I'd like to see the code :-) |
|
Ok, let's try something completely different: PR #2776 is a simpler queue with closer() ;-) |
|
Oh, I removed the branch by mistake :-p |
Add a new close() method to multiprocessing.SimpleQueue to explicitly
closes the queue. This is called automatically when the queue is
garbage collected.