Tim Peters tim.peters at gmail.com
Wed Oct 13 18:26:58 CEST 2004

[Cliff Wells]
>> This suddenly gave me a smack on the head that list.append is
>> undoubtedly written in C and might, in fact, retain the GIL for the
>> duration of the function in which case the operation might, in fact, be
>> atomic (yes, I know that isn't necessarily what the above traceback was
>> saying, but it served as a clue-stick).

[Duncan Booth]
> Roughly correct. list.append is written in C and therefore you might assume
> it is atomic. However, it increases the size of a list which means it may
> allocate memory which could cause the garbage collector to kick in which in
> turn might free up cycles which could release references to objects with
> __del__ methods which could execute other byte code at which point all bets
> are off.

Not in CPython, no.  The only things that can trigger CPython's cyclic
gc are calling gc.collect() explicitly, or (from time to time)
creating a new container object (a new object that participates in
cyclic gc).  If list.append() can't get enough memory "on the first
try" to extend the existing list, it raises MemoryError.  And if a
thread is in the bowels of list.append, the GIL prevents any other
thread from triggering cyclic GC for the duration.

More information about the Python-list mailing list