1,感謝菜鳥教程,
線程基礎:導入,創建函數,創建線和運行
import thread import time # 為線程定義一個函數 def print_time(threadName, delay): count = 0 while count < 5: time.sleep(delay) count += 1 print "%s: %s" % (threadName, time.ctime(time.time())) # 創建兩個線程 try: thread.start_new_thread(print_time, ("Thread-1", 2,)) #函數名和它的兩個參數 thread.start_new_thread(print_time, ("Thread-2", 5,)) except: print "Error: unable to start thread" while 1: pass #兩個線程會同時分別進行,一個每五秒打印一次,一個每2秒打印一次
2,threading
import threading import time exitFlag = 0 class myThread(threading.Thread): # 繼承父類 threading.Thread def __init__(self, threadID, name, counter): threading.Thread.__init__(self) self.threadID = threadID self.name = name self.counter = counter def run(self): # 把要執行的代碼寫到 run 函數里面 線程在創建后會直接運行 run 函數,所以基本可以理解為threading中,function run里的函數會自動運行 print "Starting " + self.name print_time(self.name, self.counter, 5) print "Exiting " + self.name def print_time(threadName, delay, counter): #這個是單獨的一個函數 while counter: if exitFlag: (threading.Thread).exit() time.sleep(delay) print "%s: %s" % (threadName, time.ctime(time.time())) counter -= 1 # 創建新線程 thread1 = myThread(1, "Thread-1", 1) thread2 = myThread(2, "Thread-2", 2) # 開啟線程 thread1.start() thread2.start() print "Exiting Main Thread"
3,線程鎖
import threading import time class myThread (threading.Thread): def __init__(self, threadID, name, counter): threading.Thread.__init__(self) self.threadID = threadID self.name = name self.counter = counter def run(self): print "Starting " + self.name # 上鎖 threadLock.acquire() print_time(self.name, self.counter, 3) # 釋放鎖 threadLock.release() def print_time(threadName, delay, counter): while counter: time.sleep(delay) print "%s: %s" % (threadName, time.ctime(time.time())) counter -= 1 threadLock = threading.Lock() #這個應該是實例化一個方法或者對象 threads = [] # 創建新線程 thread1 = myThread(1, "Thread-1", 1) thread2 = myThread(2, "Thread-2", 2) # 開啟新線程 thread1.start() thread2.start() # 添加線程到線程列表,注意此處,兩個線程添加到一個列表,所以先執行第一個,執行完成以后再執行第二個 threads.append(thread1) threads.append(thread2) # 等待所有線程完成,結果是兩個線程依次執行 for t in threads: t.join() print "Exiting Main Thread"
4,多個線程之間的同步用隊列
import Queue import threading import time exitFlag = 0 class myThread(threading.Thread): def __init__(self, threadID, name, q): threading.Thread.__init__(self) self.threadID = threadID self.name = name self.q = q def run(self): print "Starting " + self.name process_data(self.name, self.q) print "Exiting " + self.name def process_data(threadName, q): #實際上就是單個線程輪換着對隊列進行某個操作, while not exitFlag: queueLock.acquire() if not workQueue.empty(): data = q.get() queueLock.release() print "%s processing %s" % (threadName, data) else: queueLock.release() time.sleep(1) threadList = ["Thread-1", "Thread-2", "Thread-3"] nameList = ["One", "Two", "Three", "Four", "Five"] queueLock = threading.Lock() workQueue = Queue.Queue(10) threads = [] threadID = 1 # for循環創建新線程 for tName in threadList: thread = myThread(threadID, tName, workQueue) thread.start() threads.append(thread) threadID += 1 # 填充隊列 queueLock.acquire() for word in nameList: workQueue.put(word) queueLock.release() # 等待隊列清空 while not workQueue.empty(): pass # 通知線程是時候退出 exitFlag = 1 # 等待所有線程完成 for t in threads: t.join() print "Exiting Main Thread"
#實際上就是開三個線程清空了一個隊列,線程輪流參與,實現線程之間的同步