3 ms·
Nice! I haven't needed to pickle generators, but that should come in handy when I do.
by bcl 17y ago
Nice! I haven't needed to pickle generators, but that should come in handy when I do.
- jcl 17y agoIf I'm reading it correctly, he's not really pickling the generator itself. Instead, he's counting the number of times he called the generator and pickling that instead. Then, to "unpickle", he retrieves the count and calls the generator that many times. Two major limitations of this approach: (1) You can only pickle generators that generate the same sequence every time they are restarted. (2) All the work the generator did prior to pickling must be performed again on unpickling.
- bravura 17y agoGood criticisms. (1) You can only pickle generators that generate the same sequence every time they are restarted. I don't know how you can persist state if you do not make this assumption. (2) All the work the generator did prior to pickling must be performed again on unpickling. Something faster would be to use file.tell() to get the state and file.seek() to set the state. Since the "unpickling" is not a bottleneck, I didn't optimize this.