Immutable object thread-safety

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Mon Oct 27 03:21:40 EDT 2008


En Sun, 26 Oct 2008 23:25:09 -0200, Alcari The Mad  
<AlcariTheMad at gmail.com> escribió:

>> I am confused about which data structure to rely on thread-safety, or
>> operator in Python?
> All of the builtin functions(which are implemented in C, like len()) are
> atomic(but assigning their output to a value may not be).

You can't count on the builtins being atomic. len(x) executes  
type(x).__len__ if such method exists, which may execute arbitrary Python  
code, even trigger the garbage collector and run absolutely unrelated  
things.
See this effbot page for discussion [1] - but in general, since the  
language reference doesn't specify whether an operation is atomic or not,  
you should not count on it. Use a lock when required.

[1]  
http://effbot.org/pyfaq/what-kinds-of-global-value-mutation-are-thread-safe.htm

-- 
Gabriel Genellina




More information about the Python-list mailing list