3 ms·
The author evidently does not understand how threaded programming works in Python. The reason for the "data races" he describes is that, even with the GIL in Py
by pdonis 5y ago
The author evidently does not understand how threaded programming works in Python. The reason for the "data races" he describes is that, even with the GIL in Python, you have no control over the order in which different threads execute particular operations that are operating on the same objects, unless you use synchronization primitives like mutexes. Python has never claimed that having the GIL allows you to avoid that.
The Python documentation on threading and the GIL [1] makes it clear that the reason for the GIL is to make operations on Python objects inside the interpreter thread-safe, by ensuring that the C structures that store the states of Python objects can only be mutated by one thread at a time. All of those C-level operations happen "inside" a single bytecode. Python operations that require multiple bytecodes do not have any of the same guarantees.
[1] https://docs.python.org/3/c-api/init.html#thread-state-and-the-global-interpreter-lock https://docs.python.org/3/c-api/init.html#thread-state-and-t...
- hedora 5y agoThanks for the link. It's too bad they exposed the GIL to C extension authors. Now people will rely on it, making it impractical to remove!
- pdonis 5y ago> It's too bad they exposed the GIL to C extension authors. As I understand it, yes, that has been one of the main obstacles to removing the GIL. The current way forward seems to be to make all the interpreter state per-thread, so that each thread could have a separate interpreter; this would make the GIL unnnecessary, but of course one would still need synchronization primitives like mutexes to control access to data shared between threads.