python並行多個線程和進程


python並行多個線程和進程

工作站配置了20核CPU,平時運行程序只讓一個CPU進行運轉,浪費了很多時間。下面介紹同時啟動多個CPU運行一個程序的方法:

一個進程(CPU)包含多個線程,線程並行的python庫為threading,進程並行的庫為multiprocessing。

父進程(主函數)運行結束后,如果子進程(子函數)還沒有運行結束,需要使用join方法讓父進程等待全部結束后再結束。

下面程序中,父進程每隔30s檢查子進程的存留情況,等全部子進程運行結束后通過join結束整個程序。

並行多個線程的例子

 1 from threading import Thread #並行線程
 2 from threading import active_count
 3 from time import time, sleep
 4 
 5 def process(times,data):
 6     print("now is",times,"th process")
 7     sleep(15)
 8     f   = open(times,"w")
 9     f.write(data)
10     f.close()
11     print("OK!",times,"is finished")
12 
13 if __name__ == '__main__':
14     tstart    = time()
15     threads   = []
16     for i in range(5): #一個進程里並行5個線程
17         name='testdata{:02d}.txt'.format(i)
18         na  ='{:6.4f}'.format(i)
19         print(name)
20         t = Thread(target=process,args=(name,na))
21         threads.append(t)
22         t.setDaemon(True)
23         t.start()
24         sleep(2)
25     th_no = active_count()
26     while (th_no>1): #主線程監視間隔30s
27         sleep(30)
28         th_no = active_count()
29         trun = time()
30         print("now there are",th_no,"threads, time=",trun-tstart,"sec")
31     for t in threads:
32         t.join()
33     tend      = time()
34     print("sum up cost",tend-tstart,"sec")

 

並行多個進程的例子

CPU占用較低的小程序,並行多線程效率更高,CPU占用100%的大程序,並行多個進程效率更高,並行多個線程反而會使效率降低。

 1 from multiprocessing import Process #不同點1
 2 from multiprocessing import active_children #不同點2
 3 from time import time, sleep
 4 
 5 def process(times,data):
 6     print("now is",times,"th process")
 7     sleep(15)
 8     f   = open(times,"w")
 9     f.write(data)
10     f.close()
11     print("OK!",times,"is finished")
12 
13 if __name__ == '__main__':
14     tstart    = time()
15     threads   = []
16     for i in range(10): #數量要小於CPU核數
17         name='testdata{:02d}.txt'.format(i)
18         na  ='{:6.4f}'.format(i)
19         print(name)
20         t = Process(target=process,args=(name,na)) #與threading.Thread使用方法相似
21         threads.append(t)
22         t.start()
23         sleep(1.0)
24     pro_no = len(active_children()) #每隔30s父進程監視子進程留存數量
25     while (pro_no>1):
26         sleep(30)
27         pro_no = len(active_children())
28         trun     = time()
29         print("Running CPU NO=",pro_no," Running time=",trun-tstart,"sec")
30     for t in threads: #等待子進程全部結束開始join
31         t.join()
32     tend      = time()
33     print("sum up cost",tend-tstart,"sec")

參考官方文檔:

並行多個進程 https://docs.python.org/zh-cn/3/library/multiprocessing.html

並行多個線程 https://docs.python.org/zh-cn/3/library/threading.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM