Timer.py
Go to the documentation of this file.
1 #!/usr/bin/env/python
2 # -*- coding: euc-jp -*-
3 
4 
16 
17 
18 import time
19 import threading
20 
21 import OpenRTM_aist
22 
23 
24 
44 class Timer:
45  """
46  """
47 
48 
66  def __init__(self, interval):
67  self._interval = interval
68  self._running = False
69  self._runningMutex = threading.RLock()
70  self._tasks = []
71  self._taskMutex = threading.RLock()
72  self._thread = threading.Thread(target=self.run)
73  return
74 
75 
88  def __del__(self):
89  self._running = False
90  try:
91  self.join()
92  except:
93  pass
94  self._thread = None
95 
96  def join(self):
97  try:
98  self._thread.join()
99  self._thread = threading.Thread(target=self.run)
100  except:
101  pass
102 
103  return
104 
105 
123  def run(self):
124  while self._running:
125  self.invoke()
126  if self._interval.tv_sec != 0:
127  time.sleep(self._interval.tv_sec)
128  elif self._interval.tv_usec:
129  time.sleep(self._interval.tv_usec/1000000.0)
130  return 0
131 
132 
133 
148  def start(self):
150  if not self._running:
151  self._running = True
152  self._thread.start()
153  return
154 
155 
156 
171  def stop(self):
173  if self._running:
174  self._running = False
175  self.join()
176  return
177 
178 
179 
199  def invoke(self):
200  guard = OpenRTM_aist.ScopedLock(self._taskMutex)
201  for i in range(len(self._tasks)):
202  self._tasks[i].remains = self._tasks[i].remains - self._interval
203  if self._tasks[i].remains.sign() <= 0.0:
204  self._tasks[i].remains = self._tasks[i].period
205  self._tasks[i].listener.invoke()
206  del guard
207  return
208 
209 
241  def registerListener(self, listener, tm):
242  guard = OpenRTM_aist.ScopedLock(self._taskMutex)
243  for i in range(len(self._tasks)):
244  if self._tasks[i].listener == listener:
245  self._tasks[i].period = tm
246  self._tasks[i].remains = tm
247  return listener
248  self._tasks.append(self.Task(listener, tm))
249  return listener
250 
251 
252 
285  def registerListenerObj(self, obj, cbf, tm):
286  return self.registerListener(OpenRTM_aist.ListenerObject(obj, cbf), tm)
287 
288 
289 
315  def registerListenerFunc(self, cbf, tm):
316  return self.registerListener(OpenRTM_aist.ListenerFunc(cbf), tm)
317 
318 
319 
344  def unregisterListener(self, id):
345  guard = OpenRTM_aist.ScopedLock(self._taskMutex)
346  len_ = len(self._tasks)
347  for i in range(len_):
348  idx = (len_ - 1) - i
349  if self._tasks[idx].listener == id:
350  del self._tasks[idx]
351  return True
352  return False
353 
354 
355 
362  class Task:
363  def __init__(self, lb, tm):
364  self.listener = lb
365  self.period = tm
366  self.remains = tm
367  return
def stop(self)
Stop Timer task.
Definition: Timer.py:171
def __init__(self, interval)
Constructor.
Definition: Timer.py:66
def unregisterListener(self, id)
Unregister listener.
Definition: Timer.py:344
ListenerObject class.
Definition: Listener.py:66
def invoke(self)
Invoke Timer task.
Definition: Timer.py:199
def registerListener(self, listener, tm)
Register listener.
Definition: Timer.py:241
def registerListenerFunc(self, cbf, tm)
Register listener.
Definition: Timer.py:315
def __init__(self, lb, tm)
Definition: Timer.py:363
Timer class.
Definition: Timer.py:44
def append(dest, src)
Definition: NVUtil.py:386
def __del__(self)
Destructor.
Definition: Timer.py:88
def registerListenerObj(self, obj, cbf, tm)
Register listener.
Definition: Timer.py:285
def run(self)
Thread execution function for Timer.
Definition: Timer.py:123


openrtm_aist_python
Author(s): Shinji Kurihara
autogenerated on Mon Feb 28 2022 23:01:07