A sample thread class
| 10 | import threading |
| 11 | |
| 12 | class 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 | |
| 48 | if __name__ == "__main__": |
| 49 | testthread = TestThread() |