序列化具有属性的函数对象,加载时缺少一个属性

Serialising a function object with attributes, one attribute missing when loading

我在 python 3.7 中使用 dill,但是当我稍后重新加载它时,函数的一个属性丢失了。

我有 class 命名会话,我在程序退出时保存,在启动时加载。该对象间接包含 Tranform 个实例,这些实例具有引用特定函数的 function 属性。该函数设置了多个属性。

当我在保存会话时使用调试器时,我可以看到特定属性存在并设置为 None。但是当我加载一个保存的会话时,一切都很好,除了这个属性已经消失了。

保存代码如下:

def save(self):
    print ('\n SAVING SESSION STATE, DO NOT EXIT')
    breakpoint()

    sessionDirectory='__PETL__'
    if not os.path.exists(sessionDirectory):
        os.makedirs(sessionDirectory)
    with open(sessionDirectory+'/'+self.name, 'wb') as f: 
        dill.dump(self,f)
    print ('\nSession Saved, exiting')

加载代码如下:

def loadSession(self, sessionName):
    if (Session.dontLoad):
        print ('Creating New Session')
        return None
    try:
        with open('__PETL__/'+ sessionName, 'rb') as f:
            session=dill.load(f)
    except FileNotFoundError:
        print ('No session found, creating new one')
        return None

    return session

调试器输出如下:

节省:

> /home/osboxes/stage/inspireDataBase2/migrations/src/session/session.py(160)save()
-> sessionDirectory='__PETL__'
(Pdb) print( self.transforms[0].transform.function.queryRes)
None
(Pdb) print (dir(self.transforms[0].transform.function)[-9:])
['after', 'args', 'columns', 'fetch', 'indexs', 'query', 'queryRes', 'sameorderasafter', 'transformvar']
(Pdb) dill.dumps(self.transforms[0].transform.function)
b'\x80\x03cuserTransformModulePreparsed\ntransform__constru__buildinggeometry2d\nq\x00.'
(Pdb) c
Session Saved, exiting

正在加载:

> /home/osboxes/stage/inspireDataBase2/migrations/src/session/session.py(39)__init__()
-> session.printJobDone()
(Pdb) print( self.transforms[0].transform.function.queryRes)
*** AttributeError: 'function' object has no attribute 'queryRes'
(Pdb) print( session.transforms[0].transform.function.queryRes)
*** AttributeError: 'function' object has no attribute 'queryRes'
(Pdb) print (dir(session.transforms[0].transform.function)[-9:])
['__subclasshook__', 'after', 'args', 'columns', 'fetch', 'indexs', 'query', 'sameorderasafter', 'transformvar']

如您所见,其他属性按预期工作。

由于保存部分是我在项目中做的最后一件事,我想我只是不明白 dill 是如何工作的。该属性与另一个不同,因为这个属性是在另一个 class 中设置的(与函数不在同一模块中)。其他属性直接在函数的模块中设置。这就是说,模块是通过编译AST树得到的,但我不明白为什么会这样。

而且我确实在第一个输出中看到 dill 输出中只有对函数模块的引用(但我不知道 dill 是如何工作的,也许这是正常的)。

dill不捕获函数属性,不是针对可以直接导入的函数。您在加载时看到的任何属性都是由其他代码添加到该函数对象的,可能是在导入时。

dill.dumps() 存储的所有信息足以 re-import 相同的函数对象;在你的调试会话中是 userTransformModulePreparsed.transform__constru__buildinggeometry2d。加载该序列化时,需要做的就是 import userTransformModulePreparsed 然后使用该模块的 transform__constru__buildinggeometry2d 属性。在这种情况下,函数被视为 单例 ,每个 Python 进程只需要存在一个副本。假设该对象的所有加载都由正常的 import 进程处理。这包括添加到函数对象的属性!

dill 可以处理生成的函数对象,即任何不能直接导入的函数对象,此时它将捕获函数的所有方面,包括属性。例如,在函数(嵌套函数)内部使用 def 每次调用父函数时,总会创建一个新的、单独的函数对象。序列化此类对象的处理方式不同:

>>> import dill
>>> def foo():
...     def bar(): pass  # nested function
...     bar.spam = 'ham'
...     return bar
...
>>> foo()
<function foo.<locals>.bar at 0x110621e50>
>>> foo() is not foo()  # new calls produce new function objects
True
>>> bar = foo()
>>> vars(bar)   # the resulting function object has attributes
{'spam': 'ham'}
>>> bar_restored = dill.loads(dill.dumps(bar))
>>> vars(bar_restored)  # the attributes are preserved by dill
{'spam': 'ham'}
>>> bar.extra = 'additional'
>>> vars(dill.loads(dill.dumps(bar)))  # this extends to new attributes added later.
{'spam': 'ham', 'extra': 'additional'}

所以你在这里有两个选择;在导入时设置函数属性,在嵌套函数中生成函数。