473,586 Members | 2,870 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Queue qsize = unreliable?

I see per pydoc that Queue.Queue()'s .qsize is allegedly unreliable:

| qsize(self)
| Return the approximate size of the queue (not reliable!).

Any thoughts on why this is unreliable (and more curiously, why it would
be put in there as an unreliable function?) Rather than roll my own
threaded fifo class, it would seem prudent to use Python's built-in
Queue but the warning signs on a rather necessary function seem curious.

Jamie
Jul 18 '05 #1
4 4700
"James R. Saker Jr." <js****@america nrelay.com> writes:
I see per pydoc that Queue.Queue()'s .qsize is allegedly unreliable:

| qsize(self)
| Return the approximate size of the queue (not reliable!).

Any thoughts on why this is unreliable (and more curiously, why it would
be put in there as an unreliable function?) Rather than roll my own
threaded fifo class, it would seem prudent to use Python's built-in
Queue but the warning signs on a rather necessary function seem curious.


Well, by the time you examine (or even get!) the answer, it might be
wrong. Kinda hard to avoid this in the setting of multiple threads!

Cheers,
mwh

--
All parts should go together without forcing. You must remember that
the parts you are reassembling were disassembled by you. Therefore,
if you can't get them together again, there must be a reason. By all
means, do not use a hammer. -- IBM maintenance manual, 1925
Jul 18 '05 #2
James R. Saker Jr. wrote:
I see per pydoc that Queue.Queue()'s .qsize is allegedly unreliable:

| qsize(self)
| Return the approximate size of the queue (not reliable!).

Any thoughts on why this is unreliable (and more curiously, why it would
be put in there as an unreliable function?) Rather than roll my own
threaded fifo class, it would seem prudent to use Python's built-in
Queue but the warning signs on a rather necessary function seem curious.


(Why do you think this function is necessary? It's probably
rare to really need it, except perhaps during debugging... )

Anyway, the reason it's called "unreliable ", though the term
"inaccurate " might be more correct, is because while you are
getting the size of the queue, it might be updated such that
the new size is one or more fewer or larger than the value
that is about to be returned to you. In effect, the value is
guaranteed accurate only for the precise instant in time, now
passed, that it was determined, but by the time the calling
routine actually sees the value the size could be anything.

Note also the latest docs at docs.python.org , which state the
case a little more clearly.

"""Return the approximate size of the queue. Because of
multithreading semantics, this number is not reliable. """

-Peter
Jul 18 '05 #3
On 2004-08-06, Peter Hansen <pe***@engcorp. com> wrote:
I see per pydoc that Queue.Queue()'s .qsize is allegedly unreliable:

| qsize(self)
| Return the approximate size of the queue (not reliable!).

Any thoughts on why this is unreliable (and more curiously, why it would
be put in there as an unreliable function?) Rather than roll my own
threaded fifo class, it would seem prudent to use Python's built-in
Queue but the warning signs on a rather necessary function seem curious.


(Why do you think this function is necessary? It's probably
rare to really need it, except perhaps during debugging... )

Anyway, the reason it's called "unreliable ", though the term
"inaccurate " might be more correct, is because while you are
getting the size of the queue, it might be updated such that
the new size is one or more fewer or larger than the value
that is about to be returned to you.


I don't think that's any reason to call the function either
unreliable or inaccurate. If you're operating in a
multi-threaded environment, such a statement is trivially true
about anything that accesses shared data.

For example: time.time() needs a disclaimer that it is
unreliable, since the result it returns is incorrect by the
time you get around to using it...

--
Grant Edwards grante Yow! Spreading peanut
at butter reminds me of
visi.com opera!! I wonder why?
Jul 18 '05 #4
In article <ze************ ********@powerg ate.ca>,
Peter Hansen <pe***@engcorp. com> wrote:
James R. Saker Jr. wrote:
I see per pydoc that Queue.Queue()'s .qsize is allegedly unreliable:

| qsize(self)
| Return the approximate size of the queue (not reliable!).

Any thoughts on why this is unreliable (and more curiously, why it would
be put in there as an unreliable function?) Rather than roll my own
threaded fifo class, it would seem prudent to use Python's built-in
Queue but the warning signs on a rather necessary function seem curious.


(Why do you think this function is necessary? It's probably
rare to really need it, except perhaps during debugging... )

Anyway, the reason it's called "unreliable ", though the term
"inaccurate " might be more correct, is because while you are
getting the size of the queue, it might be updated such that
the new size is one or more fewer or larger than the value
that is about to be returned to you. In effect, the value is
guaranteed accurate only for the precise instant in time, now
passed, that it was determined, but by the time the calling
routine actually sees the value the size could be anything.

Note also the latest docs at docs.python.org , which state the
case a little more clearly.

"""Return the approximate size of the queue. Because of
multithreadi ng semantics, this number is not reliable. """


And an inaccurate number is still quite usable. If you are
the only one reading from a queue, you know that the number
is at least as large as indicated. If you read from multiple
queues, you can use the indicated number to pick which queue
to handle first.

This allows you to do pollling and fair scheduling and some other
neat tricks.

The same sort of tricks works for writers as well.

Jacob Hallén

--
Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
2248
by: Bart Nessux | last post by:
How can one view the contents of a queue? I'd like to verify that the queue has the same number of objects as the list that has been put into it. Also, should one think of a queue as static or dynamic (on the put side)? Do you put stuff in it once and then get from it until it's empty or do you put stuff in it a bit at a time? Or, can you do...
9
2772
by: phil | last post by:
And sorry I got ticked, frustrating week >And I could help more, being fairly experienced with >threading issues and race conditions and such, but >as I tried to indicate in the first place, you've >provided next to no useful (IMHO) information to >let anyone help you more than this This is about 5% of the code. Uses no locks.
4
1794
by: spinner | last post by:
--------------------------------------------- A two parter newbie question I am afraid. Am I right in thinking that using something like ... item = a_queue.get() print item will not print 'item' unless or until there is an item in the queue to
0
983
by: Kimmo Laine | last post by:
Hello, i have the following C# app, where i send messages to MSMQ: --- private void button1_Click(object sender, System.EventArgs e) { const string QNAME = @".\private$\Deltest"; MessageQueue mq = null;
2
12890
by: Arnie | last post by:
We have noticed that the FileSystemWatcher is not reliable. It is not easily repeatable but sometime it fails to catch file system changes. When it gets into this state it doesn't recover unless a reboot is done. The problem is only seen when multiple files are copied to the system in quick succession. Once the watcher misses a file *any*...
1
1576
by: Jeffrey Barish | last post by:
Several methods in Queue.Queue have warnings in their doc strings that they are not reliable (e.g., qsize). I note that the code in all these methods is bracketed with lock acquire/release. These locks are intended to protect the enclosed code from collisions with other threads. I am wondering whether I understand correctly that the reason...
2
5289
by: tikcireviva | last post by:
Hi Guys, I've done a mulithread queue implementation on stl<queue>, my developement environment is on VC6 as well as FC3. Let's talks about the win32 side. The suspected memory leak is find after I've run through my unit test cases. Test Case:
3
1182
by: Gabriel Rossetti | last post by:
Hello, I'm having some trouble with the Queue class, for some reason, if I do this (ch == ) : q = Queue.Queue(0) repr(ch) q.put(ch, True) len(q.queue)
3
8438
by: redbaron | last post by:
I run into problem with queue from multiprocessing. Even if I queue.qsize() != 0 queue.get() still blocks and queue.get_nowait() raises Emtpy error. I'm unable to cut my big part to small test case, because smaller test case similair to my real app by design is works. In what conditions is it possible? while qresult.qsize(): result =...
0
7911
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8200
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8338
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
6610
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5710
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5390
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3864
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1448
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1179
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.