atomic section in code

A

Ahmad Humayun

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,
 
F

Fredrik Lundh

Ahmad said:
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
 
A

Ahmad Humayun

Ahmad said:
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,
 
D

Diez B. Roggisch

Ahmad said:
Ahmad said:
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
 
M

Michele Simionato

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
 
F

Fredrik Lundh

Michele said:
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/python-list/2005-May/322224.html
http://mail.python.org/pipermail/python-list/2005-December/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/python-dev/2006-January/059978.html

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

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

Fredrik Lundh

Diez said:
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>
 

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,239
Messages
2,571,200
Members
47,840
Latest member
Tiffany471

Latest Threads

Top