python shell:泡菜整个状态 [英] python shell: pickle entire state

查看:87
本文介绍了python shell:泡菜整个状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用"ipython"或"code.interact(local=locals())"时,我希望有一种方法可以将整个程序地址空间保存到pickle文件中,并且类似地可以加载此类文件然后启动在这种情况下执行.

When using either "ipython" or "code.interact(local=locals())", I'd like to have a way to save the entire program address space into a pickle file, and similarly a way to load such a file and then start executing in that context.

由于虚拟内存,对于简单脚本来说,这应该是完全可能的.

This should be totally possible for simple scripts because of virtual memory.

将从本地到全局变量和全局函数的所有定义的名称(以及它们指向的非孤立对象)都将被腌制.取消选取时,这些名称将在其原始范围内重新分配.

All defined names (and the non-orphaned objects they point to), from locals up through globals and global functions, would be pickled. When unpickling, those names would be assigned again, at their original scope.

假设程序不使用网络,那么可以避免状态不连续.

Assume that the program doesn't use the network, so state discontinuity there is avoided.

假定程序在系统时钟的不一致性方面是容错的,所以在那里也没有问题.

Assume that the program is fault-tolerant with regards to the perceived discontitunity of the system clock, so no problem there either.

唯一的挑战似乎是如何处理文件描述符.理想情况下,如果尚不存在此功能,则应该有一个简单的文件描述符泡菜"功能,该功能可获取打开它的模式位,文件光标所在的位置以及文件内容的校验和(如果尝试解开密码时,校验和不匹配.

The only challenge seems to be what to do with file descriptors. Ideally, if this doesn't exist already, there should be a simple "file descriptor pickle" function that gets the mode bits it is open with, and the postion of the file cursor, and a checksum of the file contents (erroring if the checksums don't match when trying to unpickle).

是否有几行代码可以完成挑选整个会话"?

Are there a few lines that will accomplish this "pickling the entire session"?

推荐答案

为此,我将使用莳萝,可以在python中序列化几乎所有内容.

To do this, I'd use dill, which can serialize almost anything in python.

>>> import dill
>>> 
>>> def foo(a):
...   def bar(x):
...     return a*x
...   return bar
... 
>>> class baz(object):
...   def __call__(self, a,x):
...     return foo(a)(x)
... 
>>> b = baz()
>>> b(3,2)
6
>>> c = baz.__call__
>>> c(b,3,2)
6
>>> g = dill.loads(dill.dumps(globals()))
>>> g
{'dill': <module 'dill' from '/Library/Frameworks/Python.framework/Versions/7.2/lib/python2.7/site-packages/dill-0.2a.dev-py2.7.egg/dill/__init__.pyc'>, 'c': <unbound method baz.__call__>, 'b': <__main__.baz object at 0x4d61970>, 'g': {...}, '__builtins__': <module '__builtin__' (built-in)>, 'baz': <class '__main__.baz'>, '_version': '2', '__package__': None, '__name__': '__main__', 'foo': <function foo at 0x4d39d30>, '__doc__': None}

Dill将其类型注册到pickle注册表中,因此,如果您有一些使用pickle的黑匣子代码并且您不能真正对其进行编辑,那么仅导入莳萝就可以神奇地使其工作,而无需猴子修补第3方代码.

Dill registers it's types into the pickle registry, so if you have some black box code that uses pickle and you can't really edit it, then just importing dill can magically make it work without monkeypatching the 3rd party code.

您还想让整个口译人员都感到高兴……莳萝也可以做到这一点.

You also wanted to pickle the whole interpreter session... dill can do that too.

>>> # continuing from above
>>> dill.dump_session('foobar.pkl')
>>>
>>> ^D
dude@sakurai>$ python
Python 2.7.5 (default, Sep 30 2013, 20:15:49) 
[GCC 4.2.1 (Apple Inc. build 5566)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import dill
>>> dill.load_session('foobar.pkl')
>>> c(b,3,2)
6

Dill还提供了一些不错的工具来帮助您了解导致代码失败的酸洗失败的原因.

Dill also has some good tools for helping you understand what is causing your pickling to fail when your code fails.

这篇关于python shell:泡菜整个状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆