Skip to content

Instantly share code, notes, and snippets.

@whitekid
Created November 8, 2016 06:31
Show Gist options
  • Save whitekid/ee0ba6a1da8f720cb95466e9c8cebd32 to your computer and use it in GitHub Desktop.
Save whitekid/ee0ba6a1da8f720cb95466e9c8cebd32 to your computer and use it in GitHub Desktop.
synchronized decorator
from functools import wraps
class synchronized(object):
"""synchronized decorator
usage:
@synchronized(lock)
def foo():
pass
"""
def __init__(self, lock):
self.lock = lock
def __call__(self, func):
@wraps(func)
def wrap(*args, **kwargs):
with self.lock:
return func(*args, **kwargs)
return wrap
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment