我正在尝试为只读对象编写一个类,该对象不会被copy
模块真正复制,当它被腌制以在进程之间传输时,每个进程将保持不超过它的一个副本,无论它作为一个“新”对象传递多少次。是否有这样的东西?
答案 0 :(得分: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!")
答案 1 :(得分:1)
我不知道已经实现了任何此类功能。有趣的问题如下,并且需要精确的规格来确定在这种情况下会发生什么...:
如果你不在乎,说因为只有A OWNS obj - 只允许A进行更改并将obj发送给其他人,其他人不能也不会改变 - 然后问题归结为唯一地识别obj - GUID会做。该类可以维护一个类属性dict,将GUID映射到现有实例(可能作为弱值字典,以避免实例不必要地存活,但这是一个副问题)并确保在适当的时候返回现有实例。
但是如果需要将更改同步到任何更精细的粒度,那么突然间这是分布式计算的一个非常困难的问题,并且在什么情况下确实需要以极其谨慎的方式确定所发生的事情的规范(和更多的偏执比在我们大多数人中出现 - 分布式编程非常棘手,除非狂热地遵循一些简单且可证明正确的模式和习语! - 。)。
如果你可以为我们确定规格,我可以提供一个草图,说明我将如何试图满足它们。但我不会假设代表您猜测规格; - )。
编辑:OP已经澄清了,似乎他所需要的只是更好地理解如何控制__new__
。这很简单:请参阅__getnewargs__
- 您需要一个新式的类并使用协议2或更好的方法进行酸洗(但出于其他原因这些仍然是可取的! - ),然后在现有对象中__getnewargs__
可以简单地返回对象的GUID(__new__
必须作为可选参数接收)。所以__new__
可以检查GUID是否存在于类的memo
[[weakvalue ;-)]] dict中(如果是,则返回相应的对象值) - 如果不存在(或者如果GUID是没有通过,暗示它不是一个unpickling,所以必须生成一个新的GUID),然后创建一个真正的新对象(设置其GUID ;-)并将其记录在类级memo
。
顺便说一下,要制作GUID,请考虑使用标准库中的uuid模块。
答案 2 :(得分:0)
你可以简单地使用带有键的字典和接收器中相同的值。并且为避免内存泄漏,请使用WeakKeyDictionary。