Как то так, доработай напильником
import pickle,os,time
file_name='/home/smirnov/dump.dmp'
class CycleWithCounter(object):
def __init__(self, func, *args, **kw):
self.counter=0
self.func=func
self.args=args
self.kw=kw
def __iter__(self):
for i in self.func(self.counter, *self.args, **self.kw):
self.counter+=1
yield i
file=open(file_name,'w')
pickle.dump(self, file)
file.close()
def foo(start,*args, **kw):
for a in args[start:]:
yield a
time.sleep(5)
if not os.path.exists(file_name):
c_w_c=CycleWithCounter(foo, 1,2,3,4,5,6,7)
else:
file=open(file_name,'r')
c_w_c=pickle.load(file)
file.close()
for i in c_w_c:
print i
os.remove(file_name)
Первый вызов
1
2
^CTraceback (most recent call last):
File "test.py", line 47, in <module>
for i in c_w_c:
File "test.py", line 26, in __iter__
for i in self.func(self.counter, *self.args, **self.kw):
File "test.py", line 37, in foo
time.sleep(5)
KeyboardInterrupt
Второй вызов