Created
November 8, 2016 06:31
-
-
Save whitekid/ee0ba6a1da8f720cb95466e9c8cebd32 to your computer and use it in GitHub Desktop.
synchronized decorator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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