multithreading and shared dictionary

placid Bulkan at gmail.com
Fri Jul 7 09:27:42 EDT 2006


Stéphane Ninin wrote:
> Hello,
>
> Probably a stupid question, but I am not a multithreading expert...
>
> I want to share a dictionary between several threads.
> Actually, I will wrap the dictionary in a class
> and want to protect the "sensitive accesses" with locks.
>
> The problem is I am not sure which concurrent access to the dictionary
> could cause a problem.
>
> I assume that two write's on the same location would be,
> but what if one thread does
>
> mydict['a'] = something
>
>  and another thread:
>
> mydict['b'] = something else
>
> Is a lock required in such a case ?

i dont think you need to use a lock for these cases because mydict['a']
refers to mydict['b'] memory locations (i may be wrong because i dont
know Python implementation).

Just a recomendation, you could also use a Queue object to control
write access to the dictionary.

-Cheers




More information about the Python-list mailing list