【发布时间】:2019-07-05 08:44:40
【问题描述】:
我有一个非常大的迭代器。由于缺乏资源(网络、内存和时间),我无法一步执行我的程序。
所以我认为如果我将程序运行到迭代器中的第 10000 个元素然后保存它的状态会很好。下次我运行程序时,它会从迭代器中的第 10001 个元素继续。
这是我使用的代码:
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)
这个link 展示了如何在单次执行中继续迭代器。但我想要的是停止程序并再次执行。
【问题讨论】:
标签: python iterator itertools python-3.7