[Python-Dev] Re: Thread safety of deques
Paul Moore
pf_moore at yahoo.co.uk
Tue Jul 20 00:01:39 CEST 2004
"Raymond Hettinger" <python at rcn.com> writes:
> [Paul Moore]
>> The documentation for the collections.deque type states that "Deques
>> support thread-safe, memory efficient appends and pops from either
>> side of the deque". I'm not clear what the implication of saying that
>> these operations are "thread-safe" is.
>
> It is thread-safe in the same sense as the MersenneTwister's random()
> method -- all state is updated in a single step. This contrasts with
> pure python implementations which can be interrupted in the middle of
> updating state.
[...]
>> To contrast, list.append() does not claim thread safety.
>
> List operations like most C functions are also atomic.
Thanks for that clarification. A note *somewhere* in the python-level
documentation, stating that operations implemented in C are typically
atomic, and so thread-safe, might be useful.
>> This way to the slippery slope... :-)
>
> Yes. It is clearly possible to twist words into meaningless knots while
> talking about threads. Feel free to submit a clarifying doc patch.
>
> Try to keep it in proportion. It would be easy to introduce a rats nest
> of discussion about the GIL and whatnot.
Oh, definitely! Anything more than a simple paragraph would be
overkill. But some general guidance would be useful (a while ago, I
started a thread on c.l.p discussing whether d[id] = {} was
thread-safe, and at least one person stated that you should *always*
use a mutex to protect access to a shared object, which is a pretty
extreme position...)
Of course, Jython probably complicates the issue further...
> As an alternative, it may be worthwhile to submit a candidate glossary
> entry about thread-safety.
That may be a good idea. I'll try to put something together.
Paul.
PS The random quote below seems appropriate :-)
--
A little inaccuracy sometimes saves tons of explanation -- Saki
More information about the Python-Dev
mailing list