Better wrapper function exception handling

This commit is contained in:
Roberto Rosario
2012-09-27 01:54:10 -04:00
parent 2b24314d25
commit 27f0fca914
+9 -6
View File
@@ -17,15 +17,18 @@ def simple_locking(lock_id, expiration=None):
# Trying to acquire lock # Trying to acquire lock
lock = Lock.acquire_lock(lock_id, expiration) lock = Lock.acquire_lock(lock_id, expiration)
except LockError: except LockError:
# Unable to acquire lock # Unable to acquire lock - non fatal
pass pass
else: else:
# Lock acquired, proceed normally, release lock afterwards # Lock acquired, proceed normally, release lock afterwards
logger.debug('acquired lock: %s' % lock_id) logger.debug('acquired lock: %s' % lock_id)
result = function(*args, **kwargs) try:
lock.release() return function(*args, **kwargs)
return result except:
finally: # Re raise any exception that occured when calling wrapped
lock.release() # function
raise
finally:
lock.release()
return wraps(function)(wrapper) return wraps(function)(wrapper)
return inner_decorator return inner_decorator