473,395 Members | 1,726 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,395 software developers and data experts.

atomic section in code

Hi there,

I need to create an atomic section in Python code i.e. there is no
context switch to any other thread during the running of that piece of
code. Would would do the trick?
thanks,

Sep 5 '08 #1
6 5438
Ahmad Humayun wrote:
I need to create an atomic section in Python code i.e. there is no
context switch to any other thread during the running of that piece of
code. Would would do the trick?
use a lock, and make sure that anyone that needs access to the shared
state you're manipulating in that section uses the same lock.

lock = threading.Lock() # or RLock() etc [1]

with lock:
section

this only works if everyone honors the lock, of course; there's no way
in Python to lock out non-cooperating external threads.

</F>

1) see http://effbot.org/zone/thread-synchronization.htm

Sep 5 '08 #2
On Sep 5, 1:59*pm, Fredrik Lundh <fred...@pythonware.comwrote:
Ahmad Humayun wrote:
I need to create an atomic section in Python code i.e. there is no
context switch to any other thread during the running of that piece of
code. Would would do the trick?

use a lock, and make sure that anyone that needs access to the shared
state you're manipulating in that section uses the same lock.

* * *lock = threading.Lock() # or RLock() etc [1]

* * *with lock:
* * * * *section

this only works if everyone honors the lock, of course; there's no way
in Python to lock out non-cooperating external threads.

</F>

1) seehttp://effbot.org/zone/thread-synchronization.htm
Thats true, but this will ensure mutual exclusion; not atomicity

thanks again,
Sep 5 '08 #3
Ahmad Humayun schrieb:
On Sep 5, 1:59 pm, Fredrik Lundh <fred...@pythonware.comwrote:
>Ahmad Humayun wrote:
>>I need to create an atomic section in Python code i.e. there is no
context switch to any other thread during the running of that piece of
code. Would would do the trick?
use a lock, and make sure that anyone that needs access to the shared
state you're manipulating in that section uses the same lock.

lock = threading.Lock() # or RLock() etc [1]

with lock:
section

this only works if everyone honors the lock, of course; there's no way
in Python to lock out non-cooperating external threads.

</F>

1) seehttp://effbot.org/zone/thread-synchronization.htm

Thats true, but this will ensure mutual exclusion; not atomicity
AFAIC that kind of mutual exclusion is what atomicity is about. What
else do you expect to happen?

Diez
Sep 5 '08 #4
On Sep 5, 10:59*am, Fredrik Lundh <fred...@pythonware.comwrote:
>
1) seehttp://effbot.org/zone/thread-synchronization.htm
The page you link here is WAYS better than the standard documentation
of the threading module.
Generally speaking, the effbot zone contains a lot of improvements
over the standard docs
which are lacking in various areas.
I have always wondered why they are kept separated. Wouldn't be nice
to have the standard
docs integrated with the effbot docs? Are there plans in this sense
and if not, why not?

Michele Simionato
Sep 5 '08 #5
Michele Simionato wrote:
The page you link here is WAYS better than the standard documentation
of the threading module.
Generally speaking, the effbot zone contains a lot of improvements
over the standard docs which are lacking in various areas.
I have always wondered why they are kept separated. Wouldn't be nice
to have the standard
docs integrated with the effbot docs? Are there plans in this sense
and if not, why not?
There are tons of great supplementary material out there, on blogs,
personal documentation collections (such as effbot.org), cookbook sites,
etc. I don't think all that material absolutely must be posted to
python.org, but Python users would definitely benefit from improved
cross-linking.

And this is, of course, something I've lobbied for many times, e.g.

http://mail.python.org/pipermail/pyt...ay/322224.html
http://mail.python.org/pipermail/pyt...er/355625.html
http://effbot.org/zone/idea-seealso.htm

but I haven't yet figured out how to get something to happen [1]. All
ideas are welcome.

</F>

1) Well, I guess the new Sphinx tool might have gotten some inspiration
by my work in this domain:

http://mail.python.org/pipermail/pyt...ry/059978.html

and Andrew Kuchling did some preliminary work for the old Latex work flow:

http://svn.python.org/view/sandbox/trunk/seealso/

Sep 5 '08 #6
Diez B. Roggisch wrote:
AFAIC that kind of mutual exclusion is what atomicity is about. What
else do you expect to happen?
sounds like he wants/needs non-cooperative, mandatory locking.

</F>

Sep 5 '08 #7

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

Similar topics

5
by: Paul Moore | last post by:
I can't find anything which spells this out in the manuals. I guess that, at some level, the answer is "a single bytecode operation", but I'm not sure that explains it for me. This thought was...
8
by: Glenn Kasten | last post by:
I am wondering which operations in Python are guaranteed to be atomic in the presence of multi-threading. In particular, are assignment and reading of a dictionary entry atomic? For example,...
42
by: Shayan | last post by:
Is there a boolean flag that can be set atomically without needing to wrap it in a mutex? This flag will be checked constantly by multiple threads so I don't really want to deal with the overhead...
5
by: Pegboy | last post by:
What does it mean to make a function atomic? Something I read on it wasn't very clear, but made me think that I needed to disable interrupts for that function. Whether that's the case or not,...
6
by: blackstreetcat | last post by:
consider this code : int i; //gobal var Thread1: i=some value; Thread2: if (i==2) dosomething(); else dosomethingelse();
3
by: Ole Nielsby | last post by:
I need to implement reference counting in a class hierarch, in a thread safe manner. (The classes are umanaged but I might want to compile them with the /clr option.) Some of the objects - atoms...
5
by: Ark | last post by:
Consider static T foo; .............. T get_foo(void) { return foo; } In a multi-threaded (or -tasked) environment, I need to ensure that I get_foo() grabs T atomically (e.g. is not...
9
by: Dave Stallard | last post by:
Pardon if this is the wrong newsgroup for this question, and/or if this question is naive. I have a multi-threaded Windows application in which certain variables/object fields are shared: one...
2
by: Freedom fighter | last post by:
Hello, Is a singleton class the same as an atomic class? I know that a singleton class can only be instantiated once, but does that concept apply to an atomic class? Thank you.
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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...
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...

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.