RepeatedTimer now supports callback when timer stops
This commit is contained in:
parent
4f9c69965c
commit
3c5a9766c6
1 changed files with 11 additions and 2 deletions
|
|
@ -570,10 +570,13 @@ class RepeatedTimer(threading.Thread):
|
||||||
run_first (boolean): If set to True, the function will be run for the first time *before* the first wait period.
|
run_first (boolean): If set to True, the function will be run for the first time *before* the first wait period.
|
||||||
If set to False (the default), the function will be run for the first time *after* the first wait period.
|
If set to False (the default), the function will be run for the first time *after* the first wait period.
|
||||||
condition (callable): Condition that needs to be True for loop to continue. Defaults to ``lambda: True``.
|
condition (callable): Condition that needs to be True for loop to continue. Defaults to ``lambda: True``.
|
||||||
|
on_finish (callable): Callback to call when the timer finishes, either due to being cancelled or since
|
||||||
|
the condition became false.
|
||||||
daemon (bool): daemon flag to set on underlying thread.
|
daemon (bool): daemon flag to set on underlying thread.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, interval, function, args=None, kwargs=None, run_first=False, condition=None, daemon=True):
|
def __init__(self, interval, function, args=None, kwargs=None,
|
||||||
|
run_first=False, condition=None, on_finish=None, daemon=True):
|
||||||
threading.Thread.__init__(self)
|
threading.Thread.__init__(self)
|
||||||
|
|
||||||
if args is None:
|
if args is None:
|
||||||
|
|
@ -594,10 +597,11 @@ class RepeatedTimer(threading.Thread):
|
||||||
self.kwargs = kwargs
|
self.kwargs = kwargs
|
||||||
self.run_first = run_first
|
self.run_first = run_first
|
||||||
self.condition = condition
|
self.condition = condition
|
||||||
|
self.on_finish = on_finish
|
||||||
self.daemon = daemon
|
self.daemon = daemon
|
||||||
|
|
||||||
def cancel(self):
|
def cancel(self):
|
||||||
self.finished.set()
|
self.finish()
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
while self.condition():
|
while self.condition():
|
||||||
|
|
@ -618,8 +622,13 @@ class RepeatedTimer(threading.Thread):
|
||||||
# if we are to run the function AFTER waiting for the first time
|
# if we are to run the function AFTER waiting for the first time
|
||||||
self.function(*self.args, **self.kwargs)
|
self.function(*self.args, **self.kwargs)
|
||||||
|
|
||||||
|
self.finish()
|
||||||
|
|
||||||
|
def finish(self):
|
||||||
# make sure we set our finished event so we can detect that the loop was finished
|
# make sure we set our finished event so we can detect that the loop was finished
|
||||||
self.finished.set()
|
self.finished.set()
|
||||||
|
if callable(self.on_finish):
|
||||||
|
self.on_finish()
|
||||||
|
|
||||||
|
|
||||||
class CountedEvent(object):
|
class CountedEvent(object):
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue