Dictionaries and Threads
Aahz Maruch
aahz at netcom.com
Fri May 26 10:46:47 EDT 2000
In article <3926A8EF.C3C1914B at jslove.net>, Jay Love <jsliv at jslove.net> wrote:
>
>Are dictionary lookups threadsafe?
>
>ie, can I lookup and retrieve an item in a dictionary while another
>thread is adding an item?
In general yes, if you do it in single lines of code:
Thread1:
var = dict['foo']
Thread2:
dict['foo'] = 'bar'
However, if you have something like this
if dict.has_key('foo'):
var = dict['foo']
else
var = None
it is *not* thread-safe and you need to use some kind of locking
mechanism. Of course, in this specific instance, you can replace the
above code with a single line:
var = dict.get('foo',None)
Note very carefully, though, that map() and reduce() are not
thread-safe.
--
--- Aahz (Copyright 2000 by aahz at netcom.com)
Androgynous poly kinky vanilla queer het <*> http://www.rahul.net/aahz/
Hugs and backrubs -- I break Rule 6
"Yes, but would you kick any of them out of bed?"
"That depends: do we have to do anything with them in the bed, or
are they just in the bed?" -- AM/SJM
More information about the Python-list
mailing list