--
--
Every sufficiently advanced magic is indistinguishable from technology
- Arthur C Anticlarke
>
> 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.
--
Duncan Booth dun...@rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?
In addition to Duncan's excellent answer, I'd suggest that if you're
using phrases like "global variables", you probably want to learn more
about how Python's object model works in addition to learning more about
writing good threaded programs. You want to avoid sharing mutable names
across threads as much as possible.
--
Aahz (aa...@pythoncraft.com) <*> http://www.pythoncraft.com/
"It is easier to optimize correct code than to correct optimized code."
--Bill Harlan