473,397 Members | 1,972 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Because of multithreading semantics, this is not reliable.

Because of multithreading semantics, this is not reliable. This
sentence is found in the Python documentation for "7.8.1 Queue
Objects".

This scares me! Why would Queue.qsize(), Queue.empty( ), and a
Queue.full() not be reliable?

Looking at the source code of Queue.py, all 3 calls use a mutex (based
on thread.allocate_lock()). Does this mean that the
thread.allocate_lock() mechanism is not reliable (scary indeed) or does
this have to do with other implementation details?

Many thanks for explaining this mystery.

Olaf

May 3 '06 #1
11 1731
On 2006-05-03, Ol********@gmail.com <Ol********@gmail.com> wrote:
Because of multithreading semantics, this is not reliable.
This sentence is found in the Python documentation for "7.8.1
Queue Objects".

This scares me! Why would Queue.qsize(), Queue.empty( ), and a
Queue.full() not be reliable?
IIRC from the last time this question came up, what the doc
means by "not reliable" is that the result you get is accurate
at the time of the call (for the period the call is inside in
the mutex-protected region), but the saved result may not be
correct at some point in the future because some other thread
may have done an operation on the queue.

I've argued that the "not reliable" phrase is simply wrong: IMO
the calls _are_ reliable: they always return the correct value
at the time the call was made (for my previous definition of
"at the time the call was made"). That's "reliable" in my book.

I've no idea why anybody would ever expect Queue.qsize() to
return the size of the queue as it was going to be at some
undetermined point in the future.

If we were to use the "not reliable" semantics that are used in
the Queue docs, pretty much everything is "not reliable" in a
multi-threading environment. For example binding a global name
to an object is "not reliable" in a multi-threaded environment
because another thread can re-bind it later to a different
object. I think describing that problem as "global name
binding is not reliable in a multi-threaded environment" is
very misleading.
Looking at the source code of Queue.py, all 3 calls use a
mutex (based on thread.allocate_lock()). Does this mean that
the thread.allocate_lock() mechanism is not reliable (scary
indeed) or does this have to do with other implementation
details?
IMO, it has to do with a poor choice of language.
Many thanks for explaining this mystery.


No problem.

--
Grant Edwards grante Yow! Mr and Mrs PED, can I
at borrow 26.7% of the RAYON
visi.com TEXTILE production of the
INDONESIAN archipelago?
May 3 '06 #2
[Ol********@gmail.com]
Because of multithreading semantics, this is not reliable. This
sentence is found in the Python documentation for "7.8.1 Queue
Objects".

This scares me! Why would Queue.qsize(), Queue.empty( ), and a
Queue.full() not be reliable?
Because they may not be telling the truth at the instant the _caller_
tries to use the result. I'm not sure why, but people write code like

if q.empty():
return

in a thread, and then complain that "it's a bug" if some other thread
of theirs happens to sneak in and add another item to the queue
_between_ the time q.empty() correctly determined that q was empty,
and the time the code generated for "if q.empty()" tests the result.
There's no mutex to stop other threads from running between those
times. The docs could be clearer about this, and "not reliable" had a
stronger meaning in earlier versions of Python.
Looking at the source code of Queue.py, all 3 calls use a mutex (based
on thread.allocate_lock()). Does this mean that the
thread.allocate_lock() mechanism is not reliable (scary indeed)
No.
or does this have to do with other implementation details?


It just has to do with the way threads work, and with trying to
disabuse newbies of faulty common beliefs. It's good to scare
threading newbies away from these methods, because they _don't_ do
what newbies typically assume they do. That puts them in the
"attractive nuisance" category for many people.
May 3 '06 #3
Tim and Grant
if q.empty():
return


Of course you explanation is understood and ideally should be included
as a note in the Python documentation. And the "not reliable" should
be removed from the documentation!

Anyway, many thanks for your explanations (I feel "safer" now).

Olaf

May 3 '06 #4
Tim Peters wrote:
That puts them in the
"attractive nuisance" category for many people.


Argh. That gives me bad flashbacks to my torts final from Mon, which had a
bona-fide "attractive nuisance" problem on it. Damn you, Tim Peters! ;)

May 3 '06 #5
Ol********@gmail.com a écrit :
Tim and Grant
if q.empty():
return
Of course you explanation is understood and ideally should be included
as a note in the Python documentation. And the "not reliable" should
be removed from the documentation!

Anyway, many thanks for your explanations (I feel "safer" now).

Olaf


You could go as far as to say that since the function return itself
isn't wrapped in a mutex, the value can be obsolete before even the
function returns.

ie that code can sometimes return the wrong value :

def empty(self):
self.acquire_mutex()
result = self.count == 0
self.release_mutex()
return result before that line, some other thread added a value !
May 4 '06 #6
> return result before that line, some other thread added a value !

Sure, but that is the nature of using threads and a mutex. I hope you are
you not saying that every function that uses a mutex should have a comment
saying this is not "reliable"?

Olaf
May 4 '06 #7
"Olaf Meding" <Ol********@noSpam.compuserve.com> writes:
return result before that line, some other thread added a value !


Sure, but that is the nature of using threads and a mutex. I hope you are
you not saying that every function that uses a mutex should have a comment
saying this is not "reliable"?


Strictly speaking, mutex has nothing to do about it, -- if there were no
mutex there the problem would have been even worse, so this is the
nature of accessing shared resources by multiple threads.

Somewhat similar example is accessing a file after checking for its
existence in a multi-tasking OS:

if fileExists(fileName):
openFile(fileName)

has the same problem, -- at open_file time one can't rely on existence
of the file, so the check for existence is useless.

--
Sergei.

May 4 '06 #8
Olaf Meding a écrit :
return result before that line, some other thread added a value !

Sure, but that is the nature of using threads and a mutex. I hope you are
you not saying that every function that uses a mutex should have a comment
saying this is not "reliable"?


That function can return a value that was already obsolete even before
it finished executing. And so special care should be taken by the user
so that he can either cope with that or make sure that it doesn't happens.

As was said, it's the same reason the user should not do a if
fileExists() followed by a openFile. Same reason that there is a warning
in the "os.access" manual
May 4 '06 #9
On 2006-05-04, Olaf Meding <Ol********@noSpam.compuserve.com> wrote:
return result before that line, some other thread added a value !
Sure, but that is the nature of using threads and a mutex.


Yes.
I hope you are you not saying that every function that uses a
mutex should have a comment saying this is not "reliable"?


My point exactly.

--
Grant Edwards grante Yow! My pants just went to
at high school in the Carlsbad
visi.com Caverns!!!
May 4 '06 #10
Christophe
Same reason that there is a warning in the "os.access" manual


I understand the if file exists open it code.

I looked at the os.access documentation and see no "warning" or "not
reliable" wording there.
6.1.4 Files and Directories
access(path, mode)
Olaf

May 4 '06 #11
Ol********@gmail.com a écrit :
Christophe

Same reason that there is a warning in the "os.access" manual

I understand the if file exists open it code.

I looked at the os.access documentation and see no "warning" or "not
reliable" wording there.
6.1.4 Files and Directories
access(path, mode)
Olaf


6.1.4 Files and Directories

access( path, mode)

Use the real uid/gid to test for access to path. Note that most
operations will use the effective uid/gid, therefore this routine can be
used in a suid/sgid environment to test if the invoking user has the
specified access to path. mode should be F_OK to test the existence of
path, or it can be the inclusive OR of one or more of R_OK, W_OK, and
X_OK to test permissions. Return True if access is allowed, False if
not. See the Unix man page access(2) for more information. Availability:
Macintosh, Unix, Windows.
Note: Using access() to check if a user is authorized to e.g. open a
file before actually doing so using open() creates a security hole,
because the user might exploit the short time interval between checking
and opening the file to manipulate it.

I call that note a warning.
May 4 '06 #12

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

Similar topics

47
by: mihai | last post by:
What does the standard say about those two? Is any assurance that the use of STL is thread safe? Have a nice day, Mihai.
16
by: Robert Zurer | last post by:
Can anyone suggest the best book or part of a book on this subject. I'm looking for an in-depth treatment with examples in C# TIA Robert Zurer robert@zurer.com
5
by: sarge | last post by:
I would like to know how to perform simple multithreading. I had created a simple form to test out if I was multithreading properly, but got buggy results. Sometime the whole thig would lock up...
2
by: Rich | last post by:
Hello, I have set up a multithreading routine in a Test VB.net proj, and it appears to be working OK in debug mode and I am not using synchronization. Multithreading is a new thing for me, and...
5
by: sandy82 | last post by:
Whats actuallly multithreading is ... and how threading and multithreading differ . Can any1 guide how multithreading is used on the Web .. i mean a practical scenario in which u use...
1
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...
1
by: freesteel | last post by:
I have posted about this problem before. SInce then I found a much better article to help with embedding python in a multithreaded application: http://www.linuxjournal.com/article/3641 I...
7
by: Ray | last post by:
Hello, Greetings! I'm looking for a solid C++ multithreading book. Can you recommend one? I don't think I've seen a multithreading C++ book that everybody thinks is good (like Effective C++ or...
0
by: Cilk | last post by:
We at Cilk Arts are soon (Jan '09) going to release Cilk++ 1.0, with the goal of delivering the easiest, quickest, and most reliable way to maximize application performance on multicore processors....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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,...
0
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...

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.