Ivan Voras said:
Is it true what I heard (as a "rumour" of sorts), that in multithreaded
Python programs global variables are already automagically protected by
mutexes? Can someone clarify on that?
The Python interpreter holds a global mutex which is released by some
methods that call the OS, and is also released sometimes between bytecode
instructions.
The effect of this is that any operation that takes a single bytecode
instruction, and which does not call the OS, cannot be interrupted by
another Python thread. It is left to the user to work out when this means a
multithreaded operation will be safe.
For example:
x.extend(y)
If x is a list, then this is safe in a multithreaded environment. If x is a
user defined class and the extend method is coded in Python, then this
probably isn't safe.
x += y
If x and y are lists, this appears superficially to be the same as the
first example, however this code compiles to two bytecode instructions (an
inplace add and a store), so it could be interrupted with unfortunate
consequences.
The bottom line is that with care you can use a list for multi-threaded
queue operations, but with a Queue class already there and waiting it isn't
usually worth the risk.