3

I would like to work with an interpreter, then when wish to stop, save it's entire state into file. I don't want to mind what to save. For example, I don't want to list variables. I want it to automatically save everything.

Tomorrow I wish to reopen saved state and continue operating from the same place.

Various notebooks like Jupyter are not applicable, because they only can re-execute my commands to restore state, which can take time, which I wish to avoid.

Any other mature interpreting language with this capability is appreciated.

Dims
  • 47,675
  • 117
  • 331
  • 600
  • https://stackoverflow.com/questions/14519100/is-it-possible-to-save-the-python-interpreters-state-to-a-file; is this what you're looking for? mind me asking exactly what your program does? – puppydog May 07 '20 at 13:14
  • Does this answer your question? [How to save a Python interactive session?](https://stackoverflow.com/questions/947810/how-to-save-a-python-interactive-session) – mkrieger1 May 07 '20 at 13:19
  • @puppydog it states it saves exception trace. My case has no exceptions. Also, I need general thing, which works independently on what my program does. – Dims May 07 '20 at 13:25
  • @mkrieger1 I need to save state, for example, I have trained a network in variable A. I need to start with this A tomorrow. – Dims May 07 '20 at 13:26

2 Answers2

3

You can use dill to save and load python interpreter sessions using dill.dump_session and dill.load_session.

From the docs:

dill provides the ability to save the state of an interpreter session in a single command. Hence, it would be feasable to save a interpreter session, close the interpreter, ship the pickled file to another computer, open a new interpreter, unpickle the session and thus continue from the 'saved' state of the original interpreter session.

Example of using dump_session

❯ python
>>> def func(a): 
...     print(a)                                                                                                                                                                                                

>>> class MyClass: 
...     pass                                                                                                                                                                                                    

>>> x,y,z = 1, "hello", [1,2,3]                                                                                                                                                                                 

>>> import dill                                                                                                                                                                                                 

>>> dill.dump_session()

Load session:

❯ python
>>> import dill                                                                                                                                                                                                 

>>> dill.load_session()                                                                                                                                                                                         

>>> x,y,z,func,MyClass                                                                                                                                                                                 
(1, 'hello', [1, 2, 3], <function func at 0x10d853d40>, <class '__main__.MyClass'>)

dill cannot yet pickle some standard types, so you have to try yourself to see if it works for you.

hurlenko
  • 1,363
  • 2
  • 12
  • 17
  • after the load_session command I'm expecting variavles x,y,z to be available, but they are not, I get `UnboundLocalError: local variable 'x' referenced before assignment` any ideas? – ps0604 Nov 01 '21 at 15:25
  • @ps0604 Updated the example, `dump_session` wasn't called – hurlenko Nov 01 '21 at 16:05
  • I call `dump_session` and the file is generated correctly, but even after `load_session` I still get the same error message, any ideas? – ps0604 Nov 01 '21 at 16:17
0

I wrote an algorithm called a State Saving Interpreter, which saves the state of the List Prolog Interpreter (including variables and recursive states of predicates and commands) at https://github.com/luciangreen/SSI. I will use it to develop web apps and even an online Prolog interpreter. Prolog applications can be ported to the web, which needs the interpreter state to be saved to run applications.

Lucian Green
  • 182
  • 6