这里有一些简单的东西可以帮助你:
import time
import pickle
REGISTRY = None
def main(start=0):
"""Do some heavy work ..."""
global REGISTRY
a = start
while 1:
time.sleep(1)
a += 1
print a
REGISTRY = pickle.dumps(a)
if __name__ == '__main__':
print "To stop the script execution type CTRL-C"
while 1:
start = pickle.loads(REGISTRY) if REGISTRY else 0
try:
main(start=start)
except KeyboardInterrupt:
resume = raw_input('If you want to continue type the letter c:')
if resume != 'c':
break
运行示例:
$python test.py
To stop the script execution type CTRL-C
1
2
3
^CIf you want to continue type the letter c:c
4
5
6
7
8
9
^CIf you want to continue type the letter c:
$python test.py