6 ms·
You can probably trigger the behavior of random interleaving even with GIL if you use something other than list or tuple as an argument to list.extend(). CPytho
by dfox 3y ago
You can probably trigger the behavior of random interleaving even with GIL if you use something other than list or tuple as an argument to list.extend(). CPython special cases list and tuples and copies the contents directly (list_extend_fast() in listobject.c) while it uses iterator for other types (list_extend_iter()).
Because the iterator can be pretty much arbitrary Python code it seems to be a bad idea to guarantee extend() to be atomic, as you don't want to hold the list mutex while calling out into user code.
- nerdponx 3y agoAll the more reason to wonder. What's going to be special-cased to become atomic and thread-safe, and what won't be? A surprising amount of the CPython stdlib is just pure Python code.