Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## Maps class instances to threading.Locks
- INSTANCE_TO_LOCK = dict()
- ## Lock on accessing/modifying the above.
- classLockedLock = threading.Lock()
- ## Decorator function that prevents the wrapped function from executing at the
- # same time as any other similarly-wrapped function for the same class
- # instance. Assumes that the class instance is the first argument to the
- # function.
- def classLocked(func):
- def wrappedFunc(instance, *args, **kwargs):
- with classLockedLock:
- if instance not in INSTANCE_TO_LOCK:
- INSTANCE_TO_LOCK[instance] = threading.Lock()
- with INSTANCE_TO_LOCK[instance]:
- func(instance, *args, **kwargs)
- return wrappedFunc
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement