Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a Python generator be easily saved and reloaded from disk?

Is there a way to serialize a generator, current state and all (local variables, etc), so that you can load the string containing the serialized generator later and be able to pick up right from where the last yield statement exited the function? If yes and you know of a web page with a code sample, please share a link to it.

like image 275
Robert Oschler Avatar asked Jan 08 '12 14:01

Robert Oschler


2 Answers

You should check out generator_tools' picklegenerator.

You might also find this article useful: http://metaoptimize.com/blog/2009/12/22/why-cant-you-pickle-generators-in-python-workaround-pattern-for-saving-training-state/

like image 198
Acorn Avatar answered Oct 11 '22 11:10

Acorn


Stackless Python is able to pickle generators. Or you could try PyPy 1.7, which also implements many stackless features, including pickling of generators and functions.

Code snippet: http://pastebin.com/p4RkVQkJ

Stackless Python 3.2 and 2.7.2 could be downloaded here: http://zope.stackless.com/download/sdocument_view

PyPy 1.7 could be downloaded here: http://pypy.org/download.html

like image 28
Overmind Jiang Avatar answered Oct 11 '22 12:10

Overmind Jiang