473,396 Members | 2,139 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Multithreading and locking

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?

--
--
Every sufficiently advanced magic is indistinguishable from technology
- Arthur C Anticlarke
Jul 18 '05 #1
2 1734
"Ivan Voras" <iv****@fer.hr> wrote in news:bk**********@bagan.srce.hr:

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 du****@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?
Jul 18 '05 #2
In article <bk**********@bagan.srce.hr>, Ivan Voras <iv****@fer.hr> wrote:

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.
--
Aahz (aa**@pythoncraft.com) <*> http://www.pythoncraft.com/

"It is easier to optimize correct code than to correct optimized code."
--Bill Harlan
Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

47
by: mihai | last post by:
What does the standard say about those two? Is any assurance that the use of STL is thread safe? Have a nice day, Mihai.
16
by: Robert Zurer | last post by:
Can anyone suggest the best book or part of a book on this subject. I'm looking for an in-depth treatment with examples in C# TIA Robert Zurer robert@zurer.com
10
by: Marek | last post by:
Hi, I am analyzing Duwamish7 source code boundled with Visual Studio .NET 2003. Could anoybody explain why the Monitor.Enter and Monitor.Exit block is used inside a static constructor? The code...
3
by: Chris Fink | last post by:
I have written a dll that performs file IO that will be called frequently. I have been running into problems where file locking is occuring when a thread cannot access the file since it is being...
9
by: tommy | last post by:
hi, i have found a example for multithreading and asp.net http://www.fawcette.com/vsm/2002_11/magazine/features/chester/ i want to speed up my website ... if my website is starting, they...
5
by: mrkbrndck | last post by:
Please see the code below as I am trying to use multithreading for copying files to a new location in a way that improves performance of the client windows application. The problem occurs when 2...
2
by: Pradnya Patil | last post by:
hi , I am trying to draw ' html div-tag ' on the screen which will resemble a rectangle through vb.net code. I want it to be drawn faster...so I introduced multithreading using Threadpool. I...
19
by: jupiter | last post by:
Hi guys!!! Just one quick question... Which database module should I use when I want to use multi threading as my application requires lots of data from internet ???? I also want this database...
3
by: =?ISO-8859-2?Q?Krzysztof_W=B3odarczyk?= | last post by:
Hi, I think I've found a bug in Python/C API and multithreading. I'm currently creating an intrusion detection system based on mobile agents. I have an AgentPlatform (C/C++) and 2 agents on...
9
by: inxanu | last post by:
Hi all, I have several threads running (basically they are copying files from one place to another). I am using log4net for logging...However, sometimes i get a log4net error due to log file being...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.