Justin's great article, just one thing using python 2.5, makes this way easier
In Python 2.5 and later, you can also use the with statement. When used with a lock, this operator automatically obtains a lock before entering the block and releases it when leaving the block:
from future import with_statement # 2.5 only
with lock: ... access to shared resource
source share