Re:Python Concurrency: The Tricky Bits
by readmodifywrite on Thursday February 18, 2021 @11:34AM
(#26181935)
The GIL is not there to prevent data corruption on shared objects - it only protects the interpreter's internal state. The fact that sometimes you can get away with it is an accident of the GIL's implementation, not a feature anyone should rely on. It also means that you cannot rely on that behavior not to change on a successive versions of CPython.
The only safe way to do shared state between threads in CPython is locks/mutexes/message passing/etc. Even things like a simple addition to an integer are absolutely not made thread safe by the GIL.
Thanks to the GIL, multithreaded Python code doesn't have to worry too much (you do, a little) about shared mutable objects.