forked from a-rahimi/python-checkpointing2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathin_exception_handler.py
86 lines (63 loc) · 1.84 KB
/
in_exception_handler.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
"""Illustrate snapshotting a function and restarting it from an
exception handler.
"""
import copy
import logging
import function_checkpointing.save_restore as save_restore
checkpoints = []
def save_checkpoint():
ckpt = save_restore.save_jump()
if ckpt:
checkpoints.append(copy.deepcopy(ckpt))
else:
print("Checkpoint is being resumed")
checkpoints.clear()
return ckpt
def subroutine(a):
print("entering subroutine. a=%d" % a)
if not save_checkpoint():
print("Resuming from subroutine")
# raise Exception('resume exception')
print("leaving subroutine. a=%d" % a)
return 100
def processing(a, b):
lst = []
step = "step1"
lst.append(step)
print(step, "a=", a, "lst=", lst)
if not save_checkpoint():
print("Resuming from step1")
d = subroutine(a)
step = "step2"
b = 2
a *= b
lst.append(step)
print(step, "a=", a, "lst=", lst)
if not save_checkpoint():
print("Resuming from step2")
step = "step3"
c = 2
a *= c
lst.append(step)
print(step, "a=", a, "lst=", lst)
if not save_checkpoint():
print("Resuming from step3")
print("end")
def main():
logging.basicConfig()
logging.getLogger("function_checkpointing.save_restore").setLevel(logging.WARN)
logging.root.setLevel(logging.DEBUG)
print("---Run processing to completion, saving checkpoints---")
try:
raise Exception("no!")
except:
processing(a=2, b=3)
if len(checkpoints) == 4:
print("---There are 4 checkpoints. Fastforward to 2nd checkpont---")
save_restore.jump(checkpoints[1])
print("<save_restore.jump(checkpoints[1])")
else:
print("---There are only %d checkpoints now---" % len(checkpoints))
print("EXITING MAIN")
if __name__ == "__main__":
main()