2009-09-09 36 views
4

我正在尝试为只读对象编写一个类,它不会真正与copy模块一起复制,并且当它将被腌制以在进程之间传输时,每个进程将保留不超过一个副本,无论它将作为“新”对象传递多少次。有没有这样的事情?用于pickle和copy-persistent对象的类?

回答

1

我不知道已经实现了这样的功能。有趣的问题如下,并需要明确规范,什么是在这种情况下发生的...:

  • 进程A,使OBJ,并将其发送给B的unpickles它,到目前为止好
  • A向obj发送更改X,同时B将更改Y发送给目标对象的ITS副本,现在任一进程都会将其obj发送给其他对象,从而取消对其的访问:要将该对象更改为 需要在此时可见在每个过程中? A是否发送给B或反之亦然 ,即A是否“拥有”该对象?或者是什么?

如果你不在乎,比如说因为只有一个OWNS obj - 只允许A做更改并将obj发送给其他人,其他人不能也不会改变 - 那么问题归结为唯一识别obj - 一个GUID将会做。该类可以维护一个将GUID映射到现有实例的类属性dict(可能是一个弱值字典,以避免实例不必要地活着,但这是一个侧面问题),并确保在适当时返回现有实例。

但是,如果需要将更改同步到更精细的粒度,那么突然间它是分布式计算的一个非常困难的问题,以及在什么情况下需要极其小心地确定哪些情况会发生什么规格(以及更偏执于在我们大多数人中都存在 - 分布式编程是非常棘手的,除非几个简单和可证明正确的模式和习语被狂热地追随!)。

如果你能指定我们的规格,我可以提供一个草图,说明如何去尝试满足它们。但我不会以你的名义猜测这些规格;-)。

编辑:在OP澄清,似乎所有他需要的是更好地了解如何控制__new__。这很容易:请参阅__getnewargs__ - 您需要一个新式类,并使用协议2或更高版本进行酸洗(但由于其他原因,这些建议无论如何都是可行的!),然后在现有对象中使用__getnewargs__可以简单地返回对象的GUID __new__必须作为可选参数接收)。因此__new__可以检查GUID是否存在于该类的memo [[weakvalue ;-)]]字典中(如果是,则返回相应的对象值) - 如果不是(或者如果GUID未通过,则意味着它不是未删除,因此必须生成一个新的GUID),然后创建一个真正的新对象(设置其GUID ;-)并将其记录在类级别memo中。

顺便说一句,要制作GUID,请考虑在标准库中使用uuid模块。

+1

道歉,@Alex Martelli,我应该提到该对象是只读的。 – 2009-09-09 22:02:00

+1

@ cool-RR,那么我提到的方法(“一个OWNS”)应该工作(如果它是只读的,A没有理由多次发送它,所以你可以放弃我建议的每一部分,但也许“你的**”的意思是“只读”是非常奇特的,并且包括**变化......在任何我能想到的SENSIBLE解释中,这与“只读”完全相反。 ! - )。 那么,如果在我的基于GUID的建议中找不到任何内容,哦@ cool-rr? – 2009-09-10 05:49:58

+1

@Alex Martelli:是的,这几乎是我想到的方法。我试图实现它,似乎'__new__'方法是动作所在的地方,但由于文档稀缺,并且我不明白'__new__'方法是如何知道它是否不拆卸时间或正常的创作时间。 – 2009-09-10 10:01:03

0

你可以简单地使用一个与密钥和接收器中的值相同的字典。为了避免内存泄漏,请使用WeakKeyDictionary。

+0

你必须更彻底地解释。 – 2009-09-09 20:51:52

2

我试图实现这一点。 @Alex Martelli和其他人,请给我评论/改进。我认为这最终会在GitHub上结束。

""" 
todo: need to lock library to avoid thread trouble? 

todo: need to raise an exception if we're getting pickled with 
an old protocol? 

todo: make it polite to other classes that use __new__. Therefore, should 
probably work not only when there is only one item in the *args passed to new. 

""" 

import uuid 
import weakref 

library = weakref.WeakValueDictionary() 

class UuidToken(object): 
    def __init__(self, uuid): 
     self.uuid = uuid 


class PersistentReadOnlyObject(object): 
    def __new__(cls, *args, **kwargs): 
     if len(args)==1 and len(kwargs)==0 and isinstance(args[0], UuidToken): 
      received_uuid = args[0].uuid 
     else: 
      received_uuid = None 

     if received_uuid: 
      # This section is for when we are called at unpickling time 
      thing = library.pop(received_uuid, None) 
      if thing: 
       thing._PersistentReadOnlyObject__skip_setstate = True 
       return thing 
      else: # This object does not exist in our library yet; Let's add it 
       new_args = args[1:] 
       thing = super(PersistentReadOnlyObject, cls).__new__(cls, 
                    *new_args, 
                    **kwargs) 
       thing._PersistentReadOnlyObject__uuid = received_uuid 
       library[received_uuid] = thing 
       return thing 

     else: 
      # This section is for when we are called at normal creation time 
      thing = super(PersistentReadOnlyObject, cls).__new__(cls, *args, 
                   **kwargs) 
      new_uuid = uuid.uuid4() 
      thing._PersistentReadOnlyObject__uuid = new_uuid 
      library[new_uuid] = thing 
      return thing 

    def __getstate__(self): 
     my_dict = dict(self.__dict__) 
     del my_dict["_PersistentReadOnlyObject__uuid"] 
     return my_dict 

    def __getnewargs__(self): 
     return (UuidToken(self._PersistentReadOnlyObject__uuid),) 

    def __setstate__(self, state): 
     if self.__dict__.pop("_PersistentReadOnlyObject__skip_setstate", None): 
      return 
     else: 
      self.__dict__.update(state) 

    def __deepcopy__(self, memo): 
     return self 

    def __copy__(self): 
     return self 

# -------------------------------------------------------------- 
""" 
From here on it's just testing stuff; will be moved to another file. 
""" 


def play_around(queue, thing): 
    import copy 
    queue.put((thing, copy.deepcopy(thing),)) 

class Booboo(PersistentReadOnlyObject): 
    def __init__(self): 
     self.number = random.random() 

if __name__ == "__main__": 

    import multiprocessing 
    import random 
    import copy 

    def same(a, b): 
     return (a is b) and (a == b) and (id(a) == id(b)) and \ 
       (a.number == b.number) 

    a = Booboo() 
    b = copy.copy(a) 
    c = copy.deepcopy(a) 
    assert same(a, b) and same(b, c) 

    my_queue = multiprocessing.Queue() 
    process = multiprocessing.Process(target = play_around, 
             args=(my_queue, a,)) 
    process.start() 
    process.join() 
    things = my_queue.get() 
    for thing in things: 
     assert same(thing, a) and same(thing, b) and same(thing, c) 
    print("all cool!")