1.線程管理
使用模塊threading使用類Thread,
基礎格式:
1.線程的創建:
A.使用threading.Thread(target=methodname,args=())創建對象,傳入方法名和需求參數
B.定義類繼承Thread類,並改寫run()方法,再用自定義類創建對象
2.線程的啓動
.start()
3.等待線程結束.join()
join(timeout)參數指佔用CPU時間,無參默認爲佔用至線程結束。join()之後的語句在線程結束後才能執行
代碼示例:
####threading模塊用於線程控制
####主要流程:
###1.創建線程對象
###2.線程start()
###3.等待線程結束join(timeout ),timeout表示佔用CPU的時間,無參默認爲佔用至線程結束。
###join()的作用是等該線程運行完之後才執行join之後的內容,可以認爲控制線程的順序
###4.
threads=[]
import threading
import time
def printId(Id):
print("線程%d執行中..."%(Id))
time.sleep(1)
print("線程%d執行結束..."%(Id))
###1.創建線程對象
###兩種方法:1.用threading.Thread(target=methodname,args=(arg1,arg2,...))構造函數創建
###########2.繼承Thread類,並複寫其run()方法,對象start()後會自動調用run()方法執行
###########傳入的方法只能是方法名,不能帶括號
for i in range(5):
threads.append(threading.Thread(target=printId,args=(i,)))
###1.1用繼承類的方式創建線程
class MyThread(threading.Thread):
def __init__(self,id):
super(MyThread, self).__init__()
self.id=id
def run(self):
print("線程%d開始執行"%(self.id))
my_thread = MyThread(6)
##2.線程的start()
##3.線程的join()
threads[0].start()
threads[0].join()
threads[1].start()
threads[1].join()
threads[2].start()
threads[2].join()
threads[3].start()
threads[3].join()
threads[4].start()
threads[4].join()
my_thread.start()
my_thread.join()
演示結果:
線程0執行中...
線程0執行結束...
線程1執行中...
線程1執行結束...
線程2執行中...
線程2執行結束...
線程3執行中...
線程3執行結束...
線程4執行中...
線程4執行結束...
線程6開始執行
2.隊列的使用
隊列對象導入queue模塊,使用queue.Queue()的方式創建,具有先入先出的特性,可以方便線程的同步管理。
queue.get()獲取頭部
queue.put(obj)放入尾部
代碼示例:
import threading
import time
import queue
#創建工作隊員組
work_queue=queue.Queue(maxsize=10)
#創建結果接收組
result_queue=queue.Queue(maxsize=10)
class WorkerThread(threading.Thread):
def __init__(self,id):
super(WorkerThread, self).__init__()
self.id=id
def run(self):
while not work_queue.empty():
work=work_queue.get()
time.sleep(1)
out="Thread %d\t received %d"%(self.id,work)
result_queue.put(out)
def main():
for i in range(10):
work_queue.put(i)
for i in range(2):
thread=WorkerThread(i)
thread.start()
for i in range(10):
print(result_queue.get())
if __name__=="__main__":
main()
演示結果:
Thread 0 received 0
Thread 1 received 1
Thread 1 received 3
Thread 0 received 2
Thread 0 received 5
Thread 1 received 4
Thread 1 received 7
Thread 0 received 6
Thread 1 received 8
Thread 0 received 9
3.線程池
模塊:mutiprocessing.dummy
使用類:Pool
創建方法:Pool(processes=int)線程池容量
使用方法:
1.請求線程:pool.apply_async(methodname,args=())(含義同Thread的創建)
2.關閉線程池:pool.close()
3.等待線程池:pool.join()
代碼演示:
######線程池
####1.線程池的創建
####2.線程池請求線程
####3.線程池的關閉
import time
import multiprocessing.dummy
def process_func(process_id):
print("process id %d start"%(process_id))
time.sleep(3)
print("process id %d end"%(process_id))
def main():
#創建容量爲3的進程池
pool = multiprocessing.dummy.Pool(processes=3)
for i in range(10):
#向線程池中請求線程
pool.apply_async(process_func,args=(i,))
pool.close()#關閉線程池
pool.join()
if __name__=="__main__":
main()
演示結果:
可以看見當請求超出容量後,老進程關閉後,新進程纔會開始
process id 0 start
process id 1 start
process id 2 start
process id 2 end
process id 3 start
process id 1 endprocess id 0 end
process id 4 start
process id 5 start
process id 5 end
process id 6 start
process id 4 endprocess id 3 end
process id 7 start
process id 8 start
process id 7 endprocess id 6 end
process id 8 end
process id 9 start
process id 9 end