Do we want to add a "released" context manager to the threading module for
2.5? It was mentioned using the name "unlocked" in PEP 343, but never spelt out:
class released(object):
def __init__(self, lock):
self.lock = lock
def __enter__(self):
self.lock.release()
def __exit__(self, *exc_info):
self.lock.acquire()
(This context manager is the equivalent of PEP 319's asynchronize keyword)
Usage would be:
from threading import RLock, released
sync_lock = RLock()
def thread_safe():
with sync_lock:
# This is thread-safe
with released(sync_lock):
# Perform long-running or blocking operation
# that doesn't need to hold the lock
# We have the lock back here
(This particular example could be handled by two separate "with sync_lock"
statements with the asynchronous operation between them, but other cases put
the asynchronous operation inside a loop or a conditional statement which
means that particular trick won't work).
Cheers,
Nick.
--
Nick Coghlan | [EMAIL PROTECTED] | Brisbane, Australia
---------------------------------------------------------------
http://www.boredomandlaziness.org
_______________________________________________
Python-Dev mailing list
[email protected]
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe:
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com