Advertisement
Guest User

Untitled

a guest
May 22nd, 2013
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.70 KB | None | 0 0
  1. ## Maps class instances to threading.Locks
  2. INSTANCE_TO_LOCK = dict()
  3. ## Lock on accessing/modifying the above.
  4. classLockedLock = threading.Lock()
  5. ## Decorator function that prevents the wrapped function from executing at the
  6. # same time as any other similarly-wrapped function for the same class
  7. # instance. Assumes that the class instance is the first argument to the
  8. # function.
  9. def classLocked(func):
  10.     def wrappedFunc(instance, *args, **kwargs):
  11.         with classLockedLock:
  12.             if instance not in INSTANCE_TO_LOCK:
  13.                 INSTANCE_TO_LOCK[instance] = threading.Lock()
  14.         with INSTANCE_TO_LOCK[instance]:
  15.             func(instance, *args, **kwargs)
  16.     return wrappedFunc
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement