Are python objects thread-safe?

Gabriel Genellina gagsl-py2 at
Tue Dec 23 19:00:08 CET 2008

En Tue, 23 Dec 2008 11:30:25 -0200, Duncan Booth  
<duncan.booth at invalid.invalid> escribió:

> Aaron Brady <castironpi at> wrote:
>> Th.1   Th.2
>> a=X
>>        a=Y
>> a=Z
>> You are saying that if 'a=Z' interrupts 'a=Y' at the wrong time, the
>> destructor for 'X' or 'Y' might not get called.  Correct?  In serial
>> flow, the destructor for X is called, then Y.
> No, the destructors will be called, but the destructors can do pretty  
> much
> anything they want so you can't say the assignment is atomic. This isn't
> actually a threading issue: you don't need multiple threads to experience
> werid issues here. If you do strange things in a destructor then you can
> come up with confusing code even with a single thread.

A simple example showing what you said:

py> class A:
...   def __del__(self):
...     global a
...     a = None
py> a = A()
py> a = 3
py> print a

Gabriel Genellina

More information about the Python-list mailing list