What is an alternative to GIL?

Technology CommunityCategory: PythonWhat is an alternative to GIL?
VietMX Staff asked 3 years ago

The Python interpreter is not fully thread-safe. In order to support multi-threaded Python programs, there’s a global lock, called the global interpreter lock or GIL, that must be held by the current thread before it can safely access Python objects. Without the lock, even the simplest operations could cause problems in a multi-threaded program: for example, when two threads simultaneously increment the reference count of the same object, the reference count could end up being incremented only once instead of twice.

If the purpose of the GIL is to protect state from corruption, then one obvious alternative is lock at a much finer grain (fine-grained locking); perhaps at a per object level. The problem with this is that although it has been demonstrated to increase the performance of multi-threaded programs, it has more overhead and single-threaded programs suffer as a result.