Python的線程控制類
下面是一個基礎的python線程控制類
#!/usr/bin/env python """ testthread.py An example of an idiom for controling threadsDoug 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()
</pre>
本文由用戶 cgdf 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!