1+ '''Keeps running a function running even on error
2+ '''
3+
14import time
25import inspect
36
4- class KeepRunningTerminate (Exception ): pass
7+ class KeepRunningTerminate (Exception ):
8+ pass
59
610def keeprunning (wait_secs = 0 , exit_on_success = False ,
7- on_success = None , on_error = None , on_done = None ):
11+ on_success = None , on_error = None , on_done = None ):
812 '''
9- Keeps running a function running even on error.
10-
11- # Example 1: dosomething needs to run until completion condition
12- # without needing to have a loop in its code. Also, when error
13- # happens, we should NOT terminate execution
14-
13+ Example 1: dosomething needs to run until completion condition
14+ without needing to have a loop in its code. Also, when error
15+ happens, we should NOT terminate execution
16+
1517 >>> from deeputil import AttrDict
1618 >>> @keeprunning(wait_secs=1)
1719 ... def dosomething(state):
@@ -23,7 +25,8 @@ def keeprunning(wait_secs=0, exit_on_success=False,
2325 ... if state.i >= 7:
2426 ... print ("Done")
2527 ... raise keeprunning.terminate
26- ...
28+ ...
29+
2730 >>> state = AttrDict(i=0)
2831 >>> dosomething(state)
2932 AttrDict({'i': 1})
@@ -37,15 +40,14 @@ def keeprunning(wait_secs=0, exit_on_success=False,
3740 Error happened
3841 AttrDict({'i': 7})
3942 Done
40-
41-
42- # Example 2: In case you want to log exceptions while
43- # dosomething keeps running, or perform any other action
44- # when an exceptions arise
45-
43+
44+ Example 2: In case you want to log exceptions while
45+ dosomething keeps running, or perform any other action
46+ when an exceptions arise
47+
4648 >>> def some_error(__exc__):
4749 ... print (__exc__)
48- ...
50+ ...
4951 >>> @keeprunning(on_error=some_error)
5052 ... def dosomething(state):
5153 ... state.i += 1
@@ -56,8 +58,8 @@ def keeprunning(wait_secs=0, exit_on_success=False,
5658 ... if state.i >= 7:
5759 ... print ("Done")
5860 ... raise keeprunning.terminate
59- ...
60-
61+ ...
62+
6163 >>> state = AttrDict(i=0)
6264 >>> dosomething(state)
6365 AttrDict({'i': 1})
@@ -74,26 +76,26 @@ def keeprunning(wait_secs=0, exit_on_success=False,
7476 division by zero
7577 AttrDict({'i': 7})
7678 Done
77-
78- # Example 3: Full set of arguments that can be passed in @keeprunning()
79- # with class implementations
80-
79+
80+ Example 3: Full set of arguments that can be passed in @keeprunning()
81+ with class implementations
82+
8183 >>> # Class that has some class variables
8284 ... class Demo(object):
8385 ... SUCCESS_MSG = 'Yay!!'
8486 ... DONE_MSG = 'STOPPED AT NOTHING!'
8587 ... ERROR_MSG = 'Error'
86- ...
88+ ...
8789 ... # Functions to be called by @keeprunning
8890 ... def success(self):
8991 ... print((self.SUCCESS_MSG))
90- ...
92+ ...
9193 ... def failure(self, __exc__):
9294 ... print((self.ERROR_MSG, __exc__))
93- ...
95+ ...
9496 ... def task_done(self):
9597 ... print((self.DONE_MSG))
96- ...
98+ ...
9799 ... #Actual use of keeprunning with all arguments passed
98100 ... @keeprunning(wait_secs=1, exit_on_success=False,
99101 ... on_success=success, on_error=failure, on_done=task_done)
@@ -102,29 +104,31 @@ def keeprunning(wait_secs=0, exit_on_success=False,
102104 ... print (state)
103105 ... if state.i % 2 == 0:
104106 ... print("Error happened")
105- ... 1 / 0 # create an error condition
107+ ... # create an error condition
108+ ... 1 / 0
106109 ... if state.i >= 7:
107110 ... print ("Done")
108111 ... raise keeprunning.terminate
109- ...
112+ ...
113+
110114 >>> demo = Demo()
111115 >>> state = AttrDict(i=0)
112116 >>> demo.dosomething(state)
113117 AttrDict({'i': 1})
114118 Yay!!
115119 AttrDict({'i': 2})
116120 Error happened
117- ('Error', ZeroDivisionError('division by zero', ))
121+ ('Error', ZeroDivisionError('division by zero'))
118122 AttrDict({'i': 3})
119123 Yay!!
120124 AttrDict({'i': 4})
121125 Error happened
122- ('Error', ZeroDivisionError('division by zero', ))
126+ ('Error', ZeroDivisionError('division by zero'))
123127 AttrDict({'i': 5})
124128 Yay!!
125129 AttrDict({'i': 6})
126130 Error happened
127- ('Error', ZeroDivisionError('division by zero', ))
131+ ('Error', ZeroDivisionError('division by zero'))
128132 AttrDict({'i': 7})
129133 Done
130134 STOPPED AT NOTHING!
@@ -159,7 +163,7 @@ def _fn(*args, **kwargs):
159163 continue
160164
161165 _call_callback (on_success , fargs )
162-
166+
163167 _call_callback (on_done , fargs )
164168
165169 return _fn
0 commit comments