use empty method

if queue1.empty():
   pass
else:
   # get items from the queue

Note: doc says "not reliable" (no kidding).

I suppose that's because it can return True (queue is empty) while a message is posted, so the operation is not as atomic as catching the exception. Well, followed by a get_nowait() call I suppose it doesn't matter unless some other thread can consume the queue as well, in which case a racing condition could occur and the queue could be empty when you try to read from it!

Answer from Jean-François Fabre on Stack Overflow
🌐
Python
docs.python.org › 3 › library › queue.html
queue — A synchronized queue class
February 23, 2026 - Constructor for an unbounded FIFO queue. Simple queues lack advanced functionality such as task tracking. Added in version 3.7. ... Exception raised when non-blocking get() (or get_nowait()) is called on a Queue object which is empty.
🌐
Python
docs.python.org › 3 › library › asyncio-queue.html
Queues — Python 3.14.4 documentation
February 22, 2026 - A variant of Queue that retrieves most recently added entries first (last in, first out). ... This exception is raised when the get_nowait() method is called on an empty queue.
🌐
GeeksforGeeks
geeksforgeeks.org › python › queue-in-python
Queue in Python - GeeksforGeeks
December 11, 2025 - Key Methods are: put(item) / put_nowait(item) – Add an element. get() / get_nowait() – Remove an element. empty() – Check if the queue is empty. full() – Check if the queue is full.
🌐
Faucet
docs.faucet.nz › en › 1.6.15 › _modules › queue.html
queue — Python documentation
Only enqueue the item if a free slot is immediately available. Otherwise raise the Full exception. ''' return self.put(item, block=False) def get_nowait(self): '''Remove and return an item from the queue without blocking. Only get an item if one is immediately available.
🌐
GitHub
github.com › python › asyncio › issues › 265
Queue.get_nowait() and Queue.get() raise AssertionError when there are multiple tasks waiting for free slots. · Issue #265 · python/asyncio
September 23, 2015 - import asyncio queue = asyncio.Queue(2) async def putter(item): await queue.put(item) async def getter(): await asyncio.sleep(1) num = queue.qsize() try: for _ in range(num): item = queue.get_nowait() except AssertionError as e: print(e) asyncio.ensure_future(putter(0)) asyncio.ensure_future(putter(1)) asyncio.ensure_future(putter(2)) asyncio.ensure_future(putter(3)) asyncio.get_event_loop().run_until_complete(getter())
Author   manipopopo
🌐
Readthedocs
eventlet.readthedocs.io › en › latest › modules › queue.html
queue – Queue class — Eventlet 0.40.0 documentation
Remove and return an item from the queue. If optional args block is true and timeout is None (the default), block if necessary until an item is available. If timeout is a positive number, it blocks at most timeout seconds and raises the Empty exception if no item was available within that time.
Top answer
1 of 1
2

In the main program, this sequence

p.join()
obj = queue.get()

means that nothing will be pulled off of queue until p exits.

Meanwhile, the subprocess is trying to clean itself up after the function has finished. The line

q.put('X' * 1000000)

puts a string containing a million letters into the queue. The subprocess can't exit until that string has been sent.

So there's the fundamental deadlock: the main process won't read from the queue until the subprocess has exited, and the subprocess won't exit until it's written all of its items to the queue.


The other question you ask is, why does q.put('X' * 100) work?

Inside the underlying machinery of this there is some buffering. You can queue.put() and it will go into an internal buffer, but the main program will continue. The queue machinery can write some data into a pipe without it being immediately read. On the reader side, the queue machinery can read some data without it being consumed.

In all of these cases, though, it's "some" data: more than zero, but not infinite. There's some overhead that's non-zero but we can ignore for now. A 100-character string will require 100+ bytes, but probably less than 256. A million-character string will require about a megabyte.

The documentation mentions a pipe. If this goes through a Unix pipe, and the pipe buffer is, say, 32 kB, then up to 32,768 bytes can be written into the pipe without being immediately read. The short string easily fits, the low-level write(2) call returns, and the message has been "sent". If it's 1,024 kB of data, though, it doesn't all fit into the buffer, and the subprocess needs to wait for all of it to be sent. That "wait" is what leads to the deadlock.


The important TL;DR here should be that you shouldn't wait for things to be sent from the subprocess after you wait for it to complete. Do things like queue reads before you subprocess.join().

Find elsewhere
🌐
GitHub
github.com › python › cpython › blob › main › Lib › asyncio › queues.py
cpython/Lib/asyncio/queues.py at main · python/cpython
def get_nowait(self): """Remove and return an item from the queue. · Return an item if one is immediately available, else raise QueueEmpty. · Raises QueueShutDown if the queue has been shut down and is empty, or · ...
Author   python
🌐
Medium
visharma1.medium.com › python-queue-7c6533e91177
Python — Queue - Vijay Sharma
June 19, 2021 - get_nowait() — Return an element if one is immediately available, else raise QueueEmpty. ... qsize() — Return the number of items in the queue. If no free slot is immediately available, raise QueueFull.
🌐
Python
docs.python.org › 3.5 › library › asyncio-queue.html
18.5.8. Queues — Python 3.5.10 documentation
December 18, 2020 - A subclass of Queue that retrieves most recently added entries first. ... Exception raised when the get_nowait() method is called on a Queue object which is empty.
🌐
Read the Docs
python.readthedocs.io › en › latest › library › queue.html
17.7. queue — A synchronized queue class
November 15, 2017 - Constructor for an unbounded FIFO queue. Simple queues lack advanced functionality such as task tracking. Added in version 3.7. ... Exception raised when non-blocking get() (or get_nowait()) is called on a Queue object which is empty.
🌐
University of New Brunswick
cs.unb.ca › ~bremner › teaching › cs2613 › books › python3-doc › library › queue.html
queue — A synchronized queue class — Python 3.9.2 documentation
Constructor for an unbounded FIFO queue. Simple queues lack advanced functionality such as task tracking. New in version 3.7. ... Exception raised when non-blocking get() (or get_nowait()) is called on a Queue object which is empty.
🌐
Medium
medium.com › @surve.aasim › process-synchronization-using-multiprocessing-queue-4a2702cc6f5e
Process Synchronization using multiprocessing.Queue | by Aasim | Medium
August 17, 2023 - Blocking Operations: When a process attempts to extract data from an empty queue using queue.get(), it remains blocked until data becomes available. Similarly, if a process endeavors to put data into a full queue using queue.put(), it remains blocked until space is available. Non-Blocking Operations: Non-blocking alternatives include queue.put_nowait() and queue.get_nowait().
🌐
Friendliness
friendliness.dev › 2019 › 06 › 18 › python-nowait
When to use concurrent.futures.wait versus Queue.get (blocking) in Python
June 18, 2019 - def _nowait(futures): """Separate finished and unfinished threads, much like :func:`concurrent.futures.wait`, but don't wait. """ done = [] not_done = [] for future in futures: if future.done(): done.append(future) else: not_done.append(future) return done, not_done · The final implementation blocks on getting the results from the queue.
🌐
GitHub
github.com › python › cpython › blob › main › Lib › queue.py
cpython/Lib/queue.py at main · python/cpython
item = self._get() self.not_full.notify() return item · · def put_nowait(self, item): '''Put an item into the queue without blocking. · Only enqueue the item if a free slot is immediately available.
Author   python
🌐
Readthedocs
pydoc-zh.readthedocs.io › en › latest › library › queue.html
8.10. Queue — A synchronized queue class — Python 2.7.6 documentation
Queue.get_nowait()¶ · Equivalent to get(False). Two methods are offered to support tracking whether enqueued tasks have been fully processed by daemon consumer threads. Queue.task_done()¶ · Indicate that a formerly enqueued task is complete. Used by queue consumer threads.
🌐
Read the Docs
stackless.readthedocs.io › en › 2.7-slp › library › queue.html
8.10. Queue — A synchronized queue class — Stackless-Python 2.7.15 documentation
Queue.get_nowait()¶ · Equivalent to get(False). Two methods are offered to support tracking whether enqueued tasks have been fully processed by daemon consumer threads. Queue.task_done()¶ · Indicate that a formerly enqueued task is complete. Used by queue consumer threads.
🌐
Post.Byes
post.bytes.com › home › forum › topic › python
why the method get() of python Queue is hang on there? - Post.Byes
August 14, 2006 - Since queues are meant to communicate between threads - and also often to synchronize them -, the default behaviour for the get when the queue is empty is to wait for something to be put in it. If you want your second get to fail, use the get_nowait method. HTH -- python -c "print ''.join([chr(154 ...