Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving the state of a program to allow it to be resumed [duplicate]

Tags:

python

state

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?

like image 228
Dean Barnes Avatar asked Apr 06 '11 15:04

Dean Barnes


1 Answers

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

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”.

like image 118
Andrew White Avatar answered Nov 09 '22 13:11

Andrew White