Multithreading and locking

I

Ivan Voras

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?
 
D

Duncan Booth

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.
 
A

Aahz

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?

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,164
Messages
2,570,898
Members
47,439
Latest member
shasuze

Latest Threads

Top