import contextlib
import threading
import time
@contextlib.contextmanager
def cm():
try:
yield
finally:
print 'in __exit__'
def f():
with cm():
print 'in with block'
event.set()
time.sleep(10)
event = threading.Event()
t = threading.Thread(target=f)
t.daemon = True
t.start()
event.wait()
aW1wb3J0IGNvbnRleHRsaWIKaW1wb3J0IHRocmVhZGluZwppbXBvcnQgdGltZQoKQGNvbnRleHRsaWIuY29udGV4dG1hbmFnZXIKZGVmIGNtKCk6Cgl0cnk6CgkJeWllbGQKCWZpbmFsbHk6CgkJcHJpbnQgJ2luIF9fZXhpdF9fJwoKZGVmIGYoKToKCXdpdGggY20oKToKCQlwcmludCAnaW4gd2l0aCBibG9jaycKCQlldmVudC5zZXQoKQoJCXRpbWUuc2xlZXAoMTApCgpldmVudCA9IHRocmVhZGluZy5FdmVudCgpCgp0ID0gdGhyZWFkaW5nLlRocmVhZCh0YXJnZXQ9ZikKdC5kYWVtb24gPSBUcnVlCnQuc3RhcnQoKQoKZXZlbnQud2FpdCgp