15.3.8 Using locks, conditions, and semaphores in the with statement
All of the objects provided by this module that have acquire() and release() methods can be used as context managers for a with statement. The acquire() method will be called when the block is entered, and release() will be called when the block is exited.
Currently, Lock, RLock, Condition, Semaphore, and BoundedSemaphore objects may be used as with statement context managers. For example:
from __future__ import with_statement import threading some_rlock = threading.RLock() with some_rlock: print "some_rlock is locked while this executes"