4 ms·
If 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 tha
by jcl 17y ago
If 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.