Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python GIL and globals

Tags:

python

gil

In python, I have a global variable defined that gets read/incremented by different threads. Because of the GIL, will this ever cause problems without using any kind of locking mechanism?

like image 332
Bobby Brown Avatar asked Jan 22 '23 15:01

Bobby Brown


2 Answers

The GIL only requires that the interpreter completely executes a single bytecode instruction before another thread can take over. However, there is no reason to assume that an increment operation is a single instruction. For example:

>>> import dis
>>> dis.dis(compile("x=753","","exec"))
  1           0 LOAD_CONST               0 (753)
              3 STORE_NAME               0 (x)
              6 LOAD_CONST               1 (None)
              9 RETURN_VALUE
>>> dis.dis(compile("x+=1","","exec"))
  1           0 LOAD_NAME                0 (x)
              3 LOAD_CONST               0 (1)
              6 INPLACE_ADD
              7 STORE_NAME               0 (x)
             10 LOAD_CONST               1 (None)
             13 RETURN_VALUE

As you can see, even these simple operations are more than a single bytecode instruction. Therefore, whenever sharing data between threads, you must use a separate locking mechanism (eg, threading.lock) in order to maintain data consistency.

like image 131
Daniel G Avatar answered Feb 01 '23 03:02

Daniel G


Yes, multithreading without locking almost always causes problems, with or without a GIL.

like image 42
Greg Hewgill Avatar answered Feb 01 '23 03:02

Greg Hewgill