BOSWatch 3
Python Script to receive and decode German BOS Information with rtl_fm and multimon-NG
 
Loading...
Searching...
No Matches
boswatch.timer.RepeatedTimer Class Reference

Public Member Functions

def __init__ (self, interval, targetFunction, *args, **kwargs)
 Create a new instance of the RepeatedTimer.
 
def start (self)
 Start a new timer worker thread.
 
def stop (self)
 Stop the timer worker thread.
 
def isRunning (self)
 Property for repeatedTimer running state.
 
def restTime (self)
 Property to get remaining time till next call.
 

Data Fields

 overdueCount
 
 lostEvents
 

Protected Member Functions

def _target (self)
 Runs the target function with his arguments in own thread.
 

Protected Attributes

 _interval
 
 _function
 
 _args
 
 _kwargs
 
 _start
 
 _isRunning
 
 _event
 
 _thread
 

Constructor & Destructor Documentation

◆ __init__()

def boswatch.timer.RepeatedTimer.__init__ (   self,
  interval,
  targetFunction,
args,
**  kwargs 
)

Create a new instance of the RepeatedTimer.

    @param interval: interval in sec. to recall target function
    @param targetFunction: function to call on timer event
    @param *args: arguments for the called function
    @param *kwargs: keyword arguments for the called function
26 def __init__(self, interval, targetFunction, *args, **kwargs):
27 r"""!Create a new instance of the RepeatedTimer
28
29 @param interval: interval in sec. to recall target function
30 @param targetFunction: function to call on timer event
31 @param *args: arguments for the called function
32 @param *kwargs: keyword arguments for the called function
33 """
34 self._interval = interval
35 self._function = targetFunction
36 self._args = args
37 self._kwargs = kwargs
38 self._start = 0
39 self.overdueCount = 0
40 self.lostEvents = 0
41 self._isRunning = False
42 self._event = Event()
43 self._thread = None
44

Member Function Documentation

◆ start()

def boswatch.timer.RepeatedTimer.start (   self)

Start a new timer worker thread.

    @return True or False
45 def start(self):
46 r"""!Start a new timer worker thread
47
48 @return True or False"""
49 if self._thread is None:
50 self._event.clear()
51 self._thread = Thread(target=self._target)
52 self._thread.name = "RepTim(" + str(self._interval) + ")"
53 self._thread.daemon = True # start as daemon (thread dies if main program ends)
54 self._thread.start()
55 logging.debug("start repeatedTimer: %s", self._thread.name)
56 return True
57 logging.debug("repeatedTimer always started")
58 return True
59

◆ stop()

def boswatch.timer.RepeatedTimer.stop (   self)

Stop the timer worker thread.

    @return True or False
60 def stop(self):
61 r"""!Stop the timer worker thread
62
63 @return True or False"""
64 if self._thread is not None:
65 logging.debug("stop repeatedTimer: %s", self._thread.name)
66 self._event.set()
67 if self._thread is not None:
68 self._thread.join()
69 return True
70 logging.warning("repeatedTimer always stopped")
71 return True
72

◆ _target()

def boswatch.timer.RepeatedTimer._target (   self)
protected

Runs the target function with his arguments in own thread.

73 def _target(self):
74 r"""!Runs the target function with his arguments in own thread"""
75 self._start = time.time()
76 while not self._event.wait(self.restTime):
77 logging.debug("work")
78 startTime = time.time()
79
80 try:
81 self._function(*self._args, **self._kwargs)
82 except: # pragma: no cover
83 logging.exception("target throws an exception")
84
85 runTime = time.time() - startTime
86 if runTime < self._interval:
87 logging.debug("ready after: %0.3f sec. - next call in: %0.3f sec.", runTime, self.restTime)
88 else:
89 lostEvents = int(runTime / self._interval)
90 logging.warning("timer overdue! interval: %0.3f sec. - runtime: %0.3f sec. - "
91 "%d events lost - next call in: %0.3f sec.", self._interval, runTime, lostEvents, self.restTime)
92 self.lostEvents += lostEvents
93 self.overdueCount += 1
94 logging.debug("repeatedTimer thread stopped: %s", self._thread.name)
95 self._thread = None # set to none after leave teh thread (running recognize)
96

◆ isRunning()

def boswatch.timer.RepeatedTimer.isRunning (   self)

Property for repeatedTimer running state.

98 def isRunning(self):
99 r"""!Property for repeatedTimer running state"""
100 if self._thread:
101 return True
102 return False
103

◆ restTime()

def boswatch.timer.RepeatedTimer.restTime (   self)

Property to get remaining time till next call.

105 def restTime(self):
106 r"""!Property to get remaining time till next call"""
107 return self._interval - ((time.time() - self._start) % self._interval)

Field Documentation

◆ _interval

boswatch.timer.RepeatedTimer._interval
protected

◆ _function

boswatch.timer.RepeatedTimer._function
protected

◆ _args

boswatch.timer.RepeatedTimer._args
protected

◆ _kwargs

boswatch.timer.RepeatedTimer._kwargs
protected

◆ _start

boswatch.timer.RepeatedTimer._start
protected

◆ overdueCount

boswatch.timer.RepeatedTimer.overdueCount

◆ lostEvents

boswatch.timer.RepeatedTimer.lostEvents

◆ _isRunning

boswatch.timer.RepeatedTimer._isRunning
protected

◆ _event

boswatch.timer.RepeatedTimer._event
protected

◆ _thread

boswatch.timer.RepeatedTimer._thread
protected