forked from zhanglongqi/pymodslave
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RepeatTimer.py
44 lines (37 loc) · 1.37 KB
/
RepeatTimer.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
#-------------------------------------------------------------------------------
# Name: RepeatTimer
# Purpose:
#
# Author: elbar
#
# Created: 26/03/2012
# Copyright: (c) elbar 2012
# Licence: <your licence>
#-------------------------------------------------------------------------------
#!/usr/bin/env python
import threading
#add logging capability
import logging
#-------------------------------------------------------------------------------
class RepeatTimer(threading.Thread):
def __init__(self, interval, function, iterations=0, args=[], kwargs={}):
threading.Thread.__init__(self)
self._logger = logging.getLogger("modbus_tk")
self.interval = interval
self.function = function
self.iterations = iterations
self.args = args
self.kwargs = kwargs
self.finished = threading.Event()
def run(self):
count = 0
self._logger.info("Start timer")
while not self.finished.is_set() and (self.iterations <= 0 or count < self.iterations):
self.finished.wait(self.interval)
if not self.finished.is_set():
self.function(*self.args, **self.kwargs)
count += 1
self._logger.info("Stop timer")
def cancel(self):
self.finished.set()
#-------------------------------------------------------------------------------