Linked Questions

Popular Questions

How to save state of an iterator in python?

Asked by At

I have a very big iterator. due to lack of resources (Network, Memory and Time), It's not possible for me to execute my program at one step.

So I thought it would be nice if I run my program till 10000th element in iterator and then save its state. next time I run program it continues from 10001st element in iterator.

here is the code I use:

import itertools
import requests

POSSIBLE_CHARS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
URL = "URL?key={code}"

all_possible = itertools.combinations_with_replacement(POSSIBLE_CHARS, 29)
counter = itertools.count(start=1)

for c in all_possible:
    print("Try {}:".format(next(counter)), c)

    c_url = URL.format(code=c)
    resp = requests.get(c_url)
    if resp.status_code == 200:
        print("C found:", c)

        with open(c+".gif", 'b') as f:
            f.write(resp.content)

this link shows how to continue iterator inside a single execution. but what I want is to stop program and execute again.

Related Questions