Why queue.empty() returns False even after put() is called?

P

Peng Yu

Hi,

The empty() returns True even after put() has been called. Why it is
empty when there some items in it? Could anybody help me understand
it? Thanks!

~/linux/test/python/man/library/multiprocessing/Queue/empty$ cat
main.py
#!/usr/bin/env python

import multiprocessing

queue = multiprocessing.Queue()
print queue.empty()
queue.put(['a', 'b'])
queue.put(['c', 'd'])
print queue.empty()


Regards,
Peng
 
I

Ian Kelly

Hi,

The empty() returns True even after put() has been called. Why it is
empty when there some items in it? Could anybody help me understand
it? Thanks!

~/linux/test/python/man/library/multiprocessing/Queue/empty$ cat
main.py
#!/usr/bin/env python

import multiprocessing

queue = multiprocessing.Queue()
print queue.empty()
queue.put(['a', 'b'])
queue.put(['c', 'd'])
print queue.empty()

According to the docs, the Queue uses a background thread to load data into it:

When a process first puts an item on the queue a feeder thread is
started which transfers objects from a buffer into the pipe.

Most likely it still appears to be empty because this thread has not
had a chance to run yet. If you try inserting a time.sleep() call,
you should see the queue become non-empty once the background thread
has run.
 
M

MRAB

Hi,

The empty() returns True even after put() has been called. Why it is
empty when there some items in it? Could anybody help me understand
it? Thanks!

~/linux/test/python/man/library/multiprocessing/Queue/empty$ cat
main.py
#!/usr/bin/env python

import multiprocessing

queue = multiprocessing.Queue()
print queue.empty()
queue.put(['a', 'b'])
queue.put(['c', 'd'])
print queue.empty()
It works correctly for me.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,710
Latest member
bernietqt

Latest Threads

Top