Python GIL and global variables

In python, I have a global variable defined that gets read / incremented by various threads. Due to the GIL, can this cause problems without using any locking mechanisms?

+4
source share
2 answers

GIL only requires the interpreter to fully execute one bytecode instruction before another thread can take over. However, there is no reason to assume that the increment operation is a single instruction. For instance:

>>> 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 one bytecode command. Therefore, when sharing data between threads, you should use a separate locking mechanism (for example, threading.lock) to ensure data consistency.

+6
source

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

+3
source

Source: https://habr.com/ru/post/1299674/


All Articles