473,804 Members | 2,170 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 indistinguishab le from technology
- Arthur C Anticlarke
Jul 18 '05 #1
2 1747
"Ivan Voras" <iv****@fer.h r> wrote in news:bk******** **@bagan.srce.h r:

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.u k
int month(char *p){return(1248 64/((p[0]+p[1]-p[2]&0x1f)+1)%12 )["\5\x8\3"
"\6\7\xb\1\x9\x a\2\0\4"];} // Who said my code was obscure?
Jul 18 '05 #2
In article <bk**********@b agan.srce.hr>, Ivan Voras <iv****@fer.h r> 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**@pythoncra ft.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
3759
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
8514
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
2523
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 can be found in Project SystemFramework within module ApplicationLog.cs. Here comes the sample. namespace Duwamish7.SystemFramework { (some code...)
3
6240
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 used. How do I lock this IO operation down while it is being used and tell the other threads to wait until the IO is complete - like waiting in a checkout line? I have looked into numerous examples, but just cannot find anything that relates to...
9
2466
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 should build a database-connection and send a few sqls
5
2586
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 or more threads are created, the ImportOneFile method attempts to add a previously added file. If I allow 4 maximum threads and process 4 files, the last file is attempted 4 times and none of the other files are added to the destination. If I...
2
2271
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 divided the complete drawing into 3 parts..1st will be done by main thread and other two are done in these procedures - <1LongTimeTask <2LongTimeTask2 I have invoked the threads using below method. **************
19
2828
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 module to be fast, simple n efficient, in any case multi threading capabilities are # 1 requirement.
3
1576
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 it (2 Python scripts: Snort and Anomaly) These 2 agents are each running its own Python interpreter (both by
9
10410
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 used by other process. I thought about creating a method which will take care of the logging...all threads will used a delegate to call the method that does the logging static readonly object lockerLog = new object(); public delegate void...
0
9595
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10604
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10359
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10101
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7643
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6870
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5536
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5675
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3005
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.