Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
类中的Python装饰器_Python_Multithreading_Locking_Decorator - Fatal编程技术网

类中的Python装饰器

类中的Python装饰器,python,multithreading,locking,decorator,Python,Multithreading,Locking,Decorator,我想用类变量制作装饰器。 如何使decorator成为类函数? 因为我有很多需要锁定的函数。 我不想写下来 with self.lock: or self.lock.release() 每个功能。 这是我的密码 class Something: self.lock = Lock( .. ) #decorator def _lock(self, func): def wrapper(*args, **kwargs): self.

我想用类变量制作装饰器。 如何使decorator成为类函数? 因为我有很多需要锁定的函数。 我不想写下来

with self.lock: or self.lock.release() 
每个功能。 这是我的密码

class Something:
    self.lock = Lock( .. )

    #decorator
    def _lock(self, func):
        def wrapper(*args, **kwargs):
            self.lock.acquire()
            func(*args, **kwargs)
            self.lock.release()
        return wrapper

    @_lock
    def some_func(self,):
        #do something

你必须这样做。它只适用于实例方法,而不适用于函数

class Something:
    self.lock = Lock( .. )

    #decorator
    def _lock(func):
        def wrapper(self, *args, **kwargs):
            self.lock.acquire()
            r = func(self, *args, **kwargs)
            self.lock.release()
            return r
        return wrapper

    @_lock
    def some_func(self):
        #do something

类函数的含义对我来说不是很清楚。因此,我在下面的代码中显示了两个不同的锁,一个用于类方法,另一个用于实例方法

我们需要使用
try…finally
在decorator内部调用
func

def synchronized(func):
    """ Assumes that the first parameter of func has `_lock` property
    """
    def wrapper(owner, *args, **kwargs):
        owner._lock.acquire()
        try:
            return func(owner, *args, **kwargs)
        finally:
            owner._lock.release()
    return wrapper


class Something(object):
    _lock = Lock()  # for class methods

    def __init__(self):
        self._lock = Lock()  # for instance methods

    @synchronized
    def instance_method(self):
        print 'instance method invoked...'

    @classmethod
    @synchronized
    def class_method(cls):
        print 'class method invoked...'

强烈建议,如果您想要一种使用decorator使线程锁定更容易的方法,请使用
wrapt
包中的
synchronized
decorator

有关设计和rational的详细信息,请阅读以下内容。这里有太多的细节要重复

但它允许的是:

@synchronized # lock bound to function1
def function1():
    pass

@synchronized # lock bound to function2
def function2():
    pass

@synchronized # lock bound to Class
class Class(object):

    @synchronized # lock bound to instance of Class
    def function_im(self):
        pass

    @synchronized # lock bound to Class
    @classmethod
    def function_cm(cls):
        pass

    @synchronized # lock bound to function_sm
    @staticmethod
    def function_sm():
        pass
要在类的方法中实现更细粒度的锁定,还可以像上下文管理器一样使用它:

class Class(object):

    @synchronized
    def function_im_1(self):
        pass

    def function_im_2(self):
        with synchronized(self):
            pass
以及:


非常方便。这应该作为政治公众人物提交!
class Class(object):

    @synchronized
    @classmethod
    def function_cm(cls):
        pass

    def function_im(self):
        with synchronized(Class):
            pass