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
Solution 1:
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/
Solution 2:
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
Post a Comment for "Can A Python Generator Be Easily Saved And Reloaded From Disk?"