Are locks unnecessary in multi-threaded Python code because of the GIL? Are locks unnecessary in multi-threaded Python code because of the GIL? python python

Are locks unnecessary in multi-threaded Python code because of the GIL?


You will still need locks if you share state between threads. The GIL only protects the interpreter internally. You can still have inconsistent updates in your own code.

For example:

#!/usr/bin/env pythonimport threadingshared_balance = 0class Deposit(threading.Thread):    def run(self):        for _ in xrange(1000000):            global shared_balance            balance = shared_balance            balance += 100            shared_balance = balanceclass Withdraw(threading.Thread):    def run(self):        for _ in xrange(1000000):            global shared_balance            balance = shared_balance            balance -= 100            shared_balance = balancethreads = [Deposit(), Withdraw()]for thread in threads:    thread.start()for thread in threads:    thread.join()print shared_balance

Here, your code can be interrupted between reading the shared state (balance = shared_balance) and writing the changed result back (shared_balance = balance), causing a lost update. The result is a random value for the shared state.

To make the updates consistent, run methods would need to lock the shared state around the read-modify-write sections (inside the loops) or have some way to detect when the shared state had changed since it was read.


No - the GIL just protects python internals from multiple threads altering their state. This is a very low-level of locking, sufficient only to keep python's own structures in a consistent state. It doesn't cover the application level locking you'll need to do to cover thread safety in your own code.

The essence of locking is to ensure that a particular block of code is only executed by one thread. The GIL enforces this for blocks the size of a single bytecode, but usually you want the lock to span a larger block of code than this.


Adding to the discussion:

Because the GIL exists, some operations are atomic in Python and do not need a lock.

http://www.python.org/doc/faq/library/#what-kinds-of-global-value-mutation-are-thread-safe

As stated by the other answers, however, you still need to use locks whenever the application logic requires them (such as in a Producer/Consumer problem).