2 ms·
I'm so confused. from functools import wraps from threading import Lock def my_decorator(f): # Do whatever you want to set up the lock.
by Spivak 5y ago
I'm so confused.
from functools import wraps
from threading import Lock
def my_decorator(f):
# Do whatever you want to set up the lock.
lock = Lock()
@wraps(f)
def wrapper(self, *args, **kwargs):
# Do whatever with the lock.
lock.acquire()
# Do whatever you want with the instance.
print(self.greeting)
return f(self, *args, **kwargs)
return wrapper
class MyClass:
def __init__(self):
self.greeting = "Hello World!"
@my_decorator
def my_method(self):
print("And all who inhabit it.")
>>> MyClass().my_method()
Hello World!
And all who inhabit it.
Edit: Wait do you want the decorator, before it ever runs, to add a property self.lock? Are you sure you don't want this to be a class decorator that messes with __init__? Because you can 100% wrap init to do that kind of set up.
- ajkjk 5y agoThe problem is that `lock = Lock()` is shared across all instances of the class, which creates contention if there are many of them. To solve this you need to save a per-instance lock, so yes to "Wait do you want the decorator, before it ever runs, to add a property self.lock". An example usecase is writing a @cached decorator that behaves well in multithreaded programs and can be used on classes that have many instances, with the caching happening per-instance.