I'm using a modified prime generator from [here](https://stackoverflow.com/a/10733621 "here")(and [here](https://stackoverflow.com/a/19391111 "here")) and there the author suggests that it’s arbitrary where the generator starts as long as the internal data is consistent with the starting value. So I thought of extracting this data and storing it for later use. This is my modified generator:

```
from itertools import count
def SieveLoadable(level=0,PData={}):
    "modified prime sieve from http://stackoverflow.com/a/19391111"
    if not PData.get(level):#no data, start from scratch
        for i in[2, 3, 5, 7]:
            yield i
        D,C={},9
        ps=SieveLoadable(level+1)
        next(ps)
        p=next(ps)
        psq=p*p

    else:#data! load it
        D,C,psq,p=PData[level]
        ps=SieveLoadable(level+1,PData)
        while p<=7:#have next level down skip the values it gave before
            p=next(ps)

    for i in count(C,2):
        if STOP:#set this outside of the generator
        #store data in a dict outside the generator
            Internals[level]=[D,i,psq,p]
            w=next(ps)#call the next level down
            # it will hit this if statement and stop before changing its data
            break

        if i in D:
            step=D.pop(i)
        elif i<psq:
            yield i
            continue
        else:
            step=2*p
            p=next(ps)
            psq=p*p

        i+=step
        while i in D:
            i+=step
        D[i]=step

```

This works to some extent, but I’ve noticed starting and stopping makes it skip some primes (e.g. restarting it every 1 million primes makes it skip 32452883 and 32452909), so how can I make this so it doesn’t skip a single prime?

Here is how I’m calling the generator:

```
import pickle, os
PrimeFolder='C:\\Primes'
sieveData='\\'.join([PrimeFolder,"Internals.dmp"])
Internals={}
STOP=False
numPrimes = 1e6

if not os.path.exists(PrimeFolder):os.makedirs(PrimeFolder)

if os.path.exists(sieveData):#load from previous run
    File=open(sieveData,'rb')
    Internals = pickle.load(File)
    File.close()

for i,p in enumerate(SieveLoadable(0,Internals)):
    #store p in a list here

    if not i:print('Starting at: {:,}'.format(p))
    if i>=numPrimes:#amount of primes to generate at a time
        #dump list of primes to file in this if statement

        print('Stopping at: {:,}'.format(p))
        STOP=True#stop the generator

File=open(sieveData,'wb')#save for next time
pickle.dump(Internals,File)
File.close()

```

While I started with this specific prime generator, any prime generator that can dump their data and reload it for later use would be appreciated.