同步访问python对象

时间:2013-06-11 10:36:16

标签: python synchronization

我正在寻找一种通用且简单的方法来同步不使用异步调用的python类的方法。 我想到了一些可能性: 首先,在类本身的所有方法上使用装饰器:http://code.activestate.com/recipes/577105-synchronization-decorator-for-class-methods/。 但我不希望改变类,所以其次,使用包装器或子类同步访问所有子类/核心方法。 我想也许,有一种通用的方法来同步对python对象的访问,这样你就不会意外地错过层次结构中超类的方法(特别是如果以后更改它)。 所以,第三,你可以使用类似于:http://code.activestate.com/recipes/366254-generic-proxy-object-with-beforeafter-method-hooks/的通用代理 并为每次访问使用重入锁。 我更喜欢第三种选择。我只是觉得我没有找到这方面的食谱。这个解决方案还有什么问题,还是有更好的解决方案?

EDIT2: 最后一个选项看起来像下面的代码片段,并使用codetidy.com/5911/进行了测试。 测试不是证明它有效,只是一个明显的指示。由于这不是我的日常编码,如果有经验的人可以检查是否有任何错误,这将有所帮助。

#!/usr/bin/env python
import types
from pprint import pformat
from threading import RLock

class SynchronizeMethodWrapper:
    """
    Wrapper object for a method to be called.
    """
    def __init__( self, obj, func, name, rlock ):
        self.obj, self.func, self.name = obj, func, name
        self.rlock = rlock
        assert obj is not None
        assert func is not None
        assert name is not None

    def __call__( self, *args, **kwds ):
        """
        This method gets called before a method is called to sync access to the core object.
        """
        with self.rlock:
            rval = self.func(*args, **kwds)
            return rval


class SynchronizeProxy(object):
    """
    Proxy object that synchronizes access to a core object methods and attributes that don't start with _.
    """
    def __init__( self, core ):
        self._obj = core
        self.rlock = RLock()

    def __getattribute__( self, name ):
        """
        Return a proxy wrapper object if this is a method call.
        """
        if name.startswith('_'):
            return object.__getattribute__(self, name)
        else:
            att = getattr(self._obj, name)
            if type(att) is types.MethodType:
                return SynchronizeMethodWrapper(self, att, name, object.__getattribute__(self, "rlock"))
            else:
                return att

    def __setitem__( self, key, value ):
        """
        Delegate [] syntax.
        """
        name = '__setitem__'
        with self.rlock:
            att = getattr(self._obj, name)
            pmeth = SynchronizeMethodWrapper(self, att, name, self.rlock)
            pmeth(key, value)

EDIT3:我选择了SynchronizeProxy,到目前为止似乎工作正常。由于此解决方案最接近我的需求,我将选择我的答案作为解决方案

3 个答案:

答案 0 :(得分:1)

您可以使用队列来代理对该类的调用:

http://pymotw.com/2/Queue/

更新:

实际上现在我认为一个队列可能不是最好的解决方案,因为你可能必须至少调整一下这个类才能使用队列。如果您不想使用锁,可以在这里查看threading.Lock():http://docs.python.org/2/library/threading.html

答案 1 :(得分:1)

如果你真的需要,你可以使用python元类的黑魔法在类创建时动态地向类的每个方法添加一个装饰器。以下是如何执行此操作的快速示例。它创建了一个通用的同步器元类,然后您可以将其子类化为每个特定的类创建同步器。最后,您将要同步的原始类子类化,并将同步器元类应用于它。注意我正在使用python 3元类语法。

from threading import RLock

#
# Generic synchronizer
#
class SynchroMeta(type):

    def __init__(cls, name, bases, dct):
        super(SynchroMeta, cls).__init__(name, bases, dct)
        dct['__lock__'] = RLock()

        def sync_decorator(f):
            def inner(*args, **kwargs):
                with dct['__lock__']:
                    print("Synchronized call")
                    return f(*args, **kwargs)
            return inner

        for b in bases:
            if b.__name__ == cls.sync_object_name:
                for name, obj in b.__dict__.items():
                    # Synchronize any callables, but avoid special functions
                    if hasattr(obj, '__call__') and not name.startswith('__'):
                        print("Decorating: ", name)
                        setattr(b, name, sync_decorator(obj))

#
# Class you want to synchronize
#
class MyClass:
    def __init__(self, v):
        self.value = v

    def print_value(self):
        print("MyClass.value: ", self.value)

#
# Specific synchronizer for "MyClass" type
#
class MyClassSynchro(SynchroMeta):
    sync_object_name = "MyClass"


#
# Wrapper that uses the specific synchronizer metaclass
#
class MyClassWrapper(MyClass, metaclass=MyClassSynchro):
    pass


if __name__ == "__main__":
    w = MyClassWrapper('hello')
    w.print_value()

答案 2 :(得分:0)

我使用了SynchronizeProxy,它似乎到目前为止工作。由于此解决方案最接近我的需求,我将选择我的答案作为解决方案。如果我遇到任何问题,我会更新此答案。

#!/usr/bin/env python
import types
from pprint import pformat
from threading import RLock

class SynchronizeMethodWrapper:
    """
    Wrapper object for a method to be called.
    """
    def __init__( self, obj, func, name, rlock ):
        self.obj, self.func, self.name = obj, func, name
        self.rlock = rlock
        assert obj is not None
        assert func is not None
        assert name is not None

    def __call__( self, *args, **kwds ):
        """
        This method gets called before a method is called to sync access to the core object.
        """
        with self.rlock:
            rval = self.func(*args, **kwds)
            return rval


class SynchronizeProxy(object):
    """
    Proxy object that synchronizes access to a core object methods and attributes that don't start with _.
    """
    def __init__( self, core ):
        self._obj = core
        self.rlock = RLock()

    def __getattribute__( self, name ):
        """
        Return a proxy wrapper object if this is a method call.
        """
        if name.startswith('_'):
            return object.__getattribute__(self, name)
        else:
            att = getattr(self._obj, name)
            if type(att) is types.MethodType:
                return SynchronizeMethodWrapper(self, att, name, object.__getattribute__(self, "rlock"))
            else:
                return att

    def __setitem__( self, key, value ):
        """
        Delegate [] syntax.
        """
        name = '__setitem__'
        with self.rlock:
            att = getattr(self._obj, name)
            pmeth = SynchronizeMethodWrapper(self, att, name, self.rlock)
            pmeth(key, value)