thread_interruptible_loop.py
Go to the documentation of this file.
1 # This class embeds a given loop function (generally by using a decorator) into
2 # a dedicated thread, whose execution can be stoped or run on demand from the
3 # script thread.
4 #
5 # To use the previous class, a 'loop' function has to be defined.
6 # Everything will be embedded by using the decorator below. Just
7 # use it as:
8 # >>> @loopInThread
9 # >>> def Runner():
10 # >>> to what you want here
11 # >>> runner = Runner()
12 # >>> runner.pause()/play()/quit() ...
13 
14 import threading
15 import time
16 
17 from dynamic_graph.script_shortcuts import optionalparentheses
18 
19 
20 class ThreadInterruptibleLoop(threading.Thread):
21  isQuit = False
22  isPlay = False
23  sleepTime = 1e-3
24  previousHandler = None
25  isOnce = 0
26  isRunning = False
27  iter = 0
28 
29  def __init__(self):
30  threading.Thread.__init__(self)
31  self.daemon = True
32 
33  def quit(self):
34  self.isQuit = True
35 
36  def setPlay(self, mode):
37  self.isPlay = mode
38 
39  def play(self):
40  if not self.isRunning:
41  self.start()
42  self.isOnce = False
43  self.setPlay(True)
44 
45  def pause(self):
46  self.setPlay(False)
47 
48  def once(self):
49  self.isOnce = True
50  self.setPlay(True)
51 
52  def run(self):
53  self.isQuit = False
54  self.isRunning = True
55  while not self.isQuit:
56  if self.isPlay:
57  self.loop()
58  self.iter += 1
59  if self.isOnce:
60  self.pause()
61  time.sleep(self.sleepTime)
62  self.isRunning = False
63  print("Thread loop will now end.")
64 
65  def start(self):
66  self.setPlay(True)
67  threading.Thread.start(self)
68 
69  def restart(self):
70  self.join()
71  self.play()
72  self.setSigHandler()
73  threading.Thread.start(self)
74 
75  def loop(self):
76  None
77 
78 
79 # To use the previous class, a 'loop' function has to be define.
80 # Everything will be embedded by using the decorator below. Just
81 # use it as:
82 # >>> @loopInThread
83 # >>> def Runner():
84 # >>> to what you want here
85 # >>> runner = Runner()
86 # >>> runner.pause()/play()/quit() ...
87 def loopInThread(funLoop):
88  class ThreadViewer(ThreadInterruptibleLoop):
89  def __init__(self):
90  ThreadInterruptibleLoop.__init__(self)
91 
92  # self.start()
93 
94  def loop(self):
95  funLoop()
96 
97  return ThreadViewer
98 
99 
100 # Define the 4 classical shortcuts to control the loop.
101 def loopShortcuts(runner):
102  @optionalparentheses
103  def go():
104  runner.play()
105 
106  @optionalparentheses
107  def stop():
108  runner.pause()
109 
110  @optionalparentheses
111  def next():
112  runner.loop()
113 
114  class NextInc:
115  def __add__(self, x):
116  for i in range(x):
117  next()
118 
119  n = NextInc()
120  return [go, stop, next, n]
dynamic_graph.sot.core.utils.thread_interruptible_loop.ThreadInterruptibleLoop
Definition: thread_interruptible_loop.py:20
dynamic_graph.sot.core.utils.thread_interruptible_loop.ThreadInterruptibleLoop.__init__
def __init__(self)
Definition: thread_interruptible_loop.py:29
dynamic_graph.sot.core.utils.thread_interruptible_loop.ThreadInterruptibleLoop.setPlay
def setPlay(self, mode)
Definition: thread_interruptible_loop.py:36
dynamic_graph.sot.core.utils.thread_interruptible_loop.loopInThread
def loopInThread(funLoop)
Definition: thread_interruptible_loop.py:87
dynamic_graph.sot.core.utils.thread_interruptible_loop.ThreadInterruptibleLoop.isQuit
bool isQuit
Definition: thread_interruptible_loop.py:21
dynamic_graph.sot.core.utils.thread_interruptible_loop.ThreadInterruptibleLoop.once
def once(self)
Definition: thread_interruptible_loop.py:48
dynamic_graph.sot.core.utils.thread_interruptible_loop.ThreadInterruptibleLoop.sleepTime
int sleepTime
Definition: thread_interruptible_loop.py:23
dynamic_graph.sot.core.utils.thread_interruptible_loop.ThreadInterruptibleLoop.isPlay
bool isPlay
Definition: thread_interruptible_loop.py:22
dynamic_graph.sot.core.utils.thread_interruptible_loop.ThreadInterruptibleLoop.quit
def quit(self)
Definition: thread_interruptible_loop.py:33
dynamic_graph.sot.core.utils.thread_interruptible_loop.ThreadInterruptibleLoop.daemon
daemon
Definition: thread_interruptible_loop.py:31
dynamic_graph.sot.core.utils.thread_interruptible_loop.ThreadInterruptibleLoop.loop
def loop(self)
Definition: thread_interruptible_loop.py:75
dynamic_graph::script_shortcuts
dynamic_graph.sot.core.utils.thread_interruptible_loop.ThreadInterruptibleLoop.start
def start(self)
Definition: thread_interruptible_loop.py:65
dynamic_graph.sot.core.utils.thread_interruptible_loop.loopShortcuts
def loopShortcuts(runner)
Definition: thread_interruptible_loop.py:101
dynamic_graph.sot.core.utils.thread_interruptible_loop.ThreadInterruptibleLoop.isOnce
int isOnce
Definition: thread_interruptible_loop.py:25
dynamic_graph.sot.core.utils.thread_interruptible_loop.ThreadInterruptibleLoop.pause
def pause(self)
Definition: thread_interruptible_loop.py:45
dynamic_graph.sot.core.utils.thread_interruptible_loop.ThreadInterruptibleLoop.iter
int iter
Definition: thread_interruptible_loop.py:27
dynamic_graph.sot.core.utils.thread_interruptible_loop.ThreadInterruptibleLoop.restart
def restart(self)
Definition: thread_interruptible_loop.py:69
dynamic_graph.sot.core.utils.thread_interruptible_loop.ThreadInterruptibleLoop.isRunning
bool isRunning
Definition: thread_interruptible_loop.py:26
dynamic_graph.sot.core.utils.thread_interruptible_loop.ThreadInterruptibleLoop.run
def run(self)
Definition: thread_interruptible_loop.py:52
dynamic_graph.sot.core.utils.thread_interruptible_loop.ThreadInterruptibleLoop.play
def play(self)
Definition: thread_interruptible_loop.py:39


sot-core
Author(s): Olivier Stasse, ostasse@laas.fr
autogenerated on Tue Oct 24 2023 02:26:32