#from threading import Lock from multiprocessing import Lock # Set a lock and make sure it's properly released def good_way(lock): print('called good_way') with lock: raise Exception('good_way raised an exception') # Set a lock and release it in the end if no exception def bad_way(lock): print('called bad_way') lock.acquire() raise Exception('bad_way raised an exception') lock.release() if __name__=='__main__': lock = Lock() # Lock is a context funs = [good_way, bad_way] for fun in funs: print(u'\u2500'*80) # horizontal line try: fun(lock) except Exception as exc: print('Exception: ',exc) print('Trying to acquire and release the lock again') lock.acquire() lock.release() print('Got here')