MCPcopy Create free account
hub / github.com/ActiveState/code / TestThread

Class TestThread

recipes/Python/65448_Thread_Control_Idiom/recipe-65448.py:12–46  ·  view source on GitHub ↗

A sample thread class

Source from the content-addressed store, hash-verified

10import threading
11
12class TestThread(threading.Thread):
13 """
14 A sample thread class
15 """
16
17 def __init__(self):
18 """
19 Constructor, setting initial variables
20 """
21 self._stopevent = threading.Event()
22 self._sleepperiod = 1.0
23
24 threading.Thread.__init__(self, name="TestThread")
25
26 def run(self):
27 """
28 overload of threading.thread.run()
29 main control loop
30 """
31 print "%s starts" % (self.getName(),)
32
33 count = 0
34 while not self._stopevent.isSet():
35 count += 1
36 print "loop %d" % (count,)
37 self._stopevent.wait(self._sleepperiod)
38
39 print "%s ends" % (self.getName(),)
40
41 def join(self,timeout=None):
42 """
43 Stop the thread
44 """
45 self._stopevent.set()
46 threading.Thread.join(self, timeout)
47
48if __name__ == "__main__":
49 testthread = TestThread()

Callers 1

recipe-65448.pyFile · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected