保存程序状态以使其能够恢复 [英] Saving the state of a program to allow it to be resumed

查看:88
本文介绍了保存程序状态以使其能够恢复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我偶尔有需要很长时间才能运行的Python程序,并且希望能够保存其状态并在以后恢复.是否有人有聪明的方法可以每隔x秒或在程序退出时保存状态?

I occasionally have Python programs that take a long time to run, and that I want to be able to save the state of and resume later. Does anyone have a clever way of saving the state either every x seconds, or when the program is exiting?

推荐答案

将所有状态"数据放在一个位置,并使用

Put all of your "state" data in one place and use a pickle.

pickle模块实现了一个基本但功能强大的算法,用于对Python对象结构进行序列化和反序列化. "Pickling"是将Python对象层次结构转换为字节流的过程,而"unpickling"是逆运算,从而字节流被转换回对象层次结构的过程.酸洗(和酸洗)也称为序列化",编组", 1 或但是,为了避免混淆,拉平"是指酸洗"和未酸洗".

The pickle module implements a fundamental, but powerful algorithm for serializing and de-serializing a Python object structure. "Pickling" is the process whereby a Python object hierarchy is converted into a byte stream, and "unpickling" is the inverse operation, whereby a byte stream is converted back into an object hierarchy. Pickling (and unpickling) is alternatively known as "serialization", "marshalling," 1 or "flattening", however, to avoid confusion, the terms used here are "pickling" and "unpickling".

这篇关于保存程序状态以使其能够恢复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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