available, else raise the :exc:`queue.Full` exception (*timeout* is
ignored in that case).
+ .. versionchanged:: 3.8
+ If the queue is closed, :exc:`ValueError` is raised instead of
+ :exc:`AssertionError`.
+
.. method:: put_nowait(obj)
Equivalent to ``put(obj, False)``.
``False``), return an item if one is immediately available, else raise the
:exc:`queue.Empty` exception (*timeout* is ignored in that case).
+ .. versionchanged:: 3.8
+ If the queue is closed, :exc:`ValueError` is raised instead of
+ :exc:`OSError`.
+
.. method:: get_nowait()
Equivalent to ``get(False)``.
self._poll = self._reader.poll
def put(self, obj, block=True, timeout=None):
- assert not self._closed, "Queue {0!r} has been closed".format(self)
+ if self._closed:
+ raise ValueError(f"Queue {self!r} is closed")
if not self._sem.acquire(block, timeout):
raise Full
self._notempty.notify()
def get(self, block=True, timeout=None):
+ if self._closed:
+ raise ValueError(f"Queue {self!r} is closed")
if block and timeout is None:
with self._rlock:
res = self._recv_bytes()
self._cond, self._unfinished_tasks = state[-2:]
def put(self, obj, block=True, timeout=None):
- assert not self._closed, "Queue {0!r} is closed".format(self)
+ if self._closed:
+ raise ValueError(f"Queue {self!r} is closed")
if not self._sem.acquire(block, timeout):
raise Full
# Assert that the serialization and the hook have been called correctly
self.assertTrue(not_serializable_obj.reduce_was_called)
self.assertTrue(not_serializable_obj.on_queue_feeder_error_was_called)
+
+ def test_closed_queue_put_get_exceptions(self):
+ for q in multiprocessing.Queue(), multiprocessing.JoinableQueue():
+ q.close()
+ with self.assertRaisesRegex(ValueError, 'is closed'):
+ q.put('foo')
+ with self.assertRaisesRegex(ValueError, 'is closed'):
+ q.get()
#
#
#
--- /dev/null
+When the queue is closed, :exc:`ValueError` is now raised by
+:meth:`multiprocessing.Queue.put` and :meth:`multiprocessing.Queue.get`
+instead of :exc:`AssertionError` and :exc:`OSError`, respectively.
+Patch by Zackery Spytz.