Why Do I have to worry about Thread Safety in CPython?
<p>From what I understand, the Global Interpreter Lock allows only a single thread to access the interpreter and execute bytecode. If that's the case, then at any given time, only a single thread will be using the interpreter and its memory. </p>

<p>With that I believe that it is fair to exclude the possibility of having race cases, since no two threads can access the interpreter's memory at the same time, yet I still see warnings about making sure data structures are "thread safe". There is a possibility that it may be covering all implementations of the python interpreter (like cython) which can switch off the GIL and allow true multi threading. </p>

<p>I understand the importance of thread safety in interpreter environments that do not have the GIL enabled. However, for CPython, why is thread safety encouraged when writing multi threaded python code? What is the worse that can happen in the CPython environment? </p>

<p>Of course race conditions can still take place, because access to datastructures is <em>not atomic</em>.</p>

<p>Say you test for a key being present in a dictionary, then do something to add the key:</p>

<pre><code>if key not in dictionary:
    # calculate new value
    value = elaborate_calculation()
    dictionary[key] = value
</code></pre>

<p>The thread can be switched at any point after the <code>not in</code> test has returned true, and <em>another thread</em> will also come to the conclusion that the key isn't there. Now two threads are doing the calculation, and you don't know which one will win.</p>

<p>All that the GIL does is protect Python's <em>internal interpreter state</em>. This doesn't mean that data structures used by Python code itself are now locked and protected.</p>

python multithreading thread-safety cpython gil