atomic section in code
Fredrik Lundh
fredrik at pythonware.com
Fri Sep 5 04:59:52 EDT 2008
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
More information about the Python-list
mailing list