Skip to content
Snippets Groups Projects
Commit 04ad709c authored by David Trattnig's avatar David Trattnig
Browse files

Add new @synchronized decorator #65

parent 3953e9b2
No related branches found
No related tags found
No related merge requests found
......@@ -18,16 +18,40 @@
"""
A collection of Python meta-programming and language utilities.
A collection of meta-programming and language utilities.
"""
import inspect
from functools import wraps
from multiprocessing import Lock
def synchronized(member):
"""
@synchronized decorator.
Lock a method for synchronized access only.
"""
mutex = Lock()
@wraps(member)
def wrapper(*args, **vargs):
result = ""
try:
mutex.acquire()
result = member(*args, **vargs)
mutex.release()
except Exception as e:
mutex.release()
raise e
return result
return wrapper
def private(member):
"""
@private Decorator.
@private decorator.
Use this to annotate your methods for private-visibility.
......@@ -35,7 +59,7 @@ def private(member):
"""
@wraps(member)
def wrapper(*args):
def wrapper(*args, **vargs):
me = member.__name__
stack = inspect.stack()
calling_class = stack[1][0].f_locals["self"].__class__.__name__
......@@ -44,7 +68,7 @@ def private(member):
msg = f'"{me}(..)" called by "{calling_class}.{calling_method}(..)" is private'
print(msg)
raise Exception(msg)
return member(*args)
return member(*args, **vargs)
return wrapper
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment