gpt4 book ai didi

python - pickle 和复制持久对象的类?

转载 作者:太空宇宙 更新时间:2023-11-03 11:11:41 26 4
gpt4 key购买 nike

我正在尝试为只读对象编写一个类,该类不会使用 copy 模块进行真正复制,并且当它被 pickle 以在进程之间传输时,每个进程将维护最多只有一个副本,无论它作为"new"对象传递多少次。已经有类似的东西了吗?

最佳答案

我试图实现这一点。 @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!")

关于python - pickle 和复制持久对象的类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1400295/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com