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 | ncoghlan at gmail.com | Brisbane, Australia --------------------------------------------------------------- http://www.boredomandlaziness.org
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4