下面是一个基础的python线程控制类
#!/usr/bin/env python
"""
testthread.py
an example of an idiom for controling threads
doug fort
http://www.dougfort.net
"""
import threading
class testthread(threading.thread):
"""
a sample thread class
"""
def __init__(self):
"""
constructor, setting initial variables
"""
self._stopevent = threading.event()
self._sleepperiod = 1.0
threading.thread.__init__(self, name="testthread")
def run(self):
"""
overload of threading.thread.run()
main control loop
"""
print "%s starts" % (self.getname(),)
count = 0
while not self._stopevent.isset():
count = 1
print "loop %d" % (count,)
self._stopevent.wait(self._sleepperiod)
print "%s ends" % (self.getname(),)
def join(self,timeout=none):
"""
stop the thread
"""
self._stopevent.set()
threading.thread.join(self, timeout)
if __name__ == "__main__":
testthread = testthread()
testthread.start()
import time
time.sleep(10.0)
testthread.join()
我是段花花花花花花