Are python objects thread-safe?

James Mills prologic at shortcircuit.net.au
Sun Dec 21 17:27:48 EST 2008


On Mon, Dec 22, 2008 at 4:51 AM, RajNewbie <raj.indian.08 at gmail.com> wrote:
> Say, I have two threads, updating the same dictionary object - but for
> different parameters:
> Please find an example below:
> a = {file1Data : '',
>       file2Data : ''}
>
> Now, I send it to two different threads, both of which are looping
> infinitely:
> In thread1:
> a['file1Data'] = open(filename1).read
>          and
> in thread2:
> a['file2Data'] = open(filename2).read
>
> My question is  - is this object threadsafe? - since we are working on
> two different parameters in the object. Or should I have to block the
> whole object?

I believe (iirc), all basic data types
and objects are thread-safe. I could
be wrong though - I don't tend to
use threads much myself :)

cheers
James



More information about the Python-list mailing list