您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關如何在Python3.5中使用多進程,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。
具體如下:
進程類:Process
示例及代碼:
(1)創建函數作為單進程
#!/usr/bin/env python # -*- coding:utf-8 -*- # Author:ZhengzhengLiu import multiprocessing import time #創建函數并將其作為單個進程 def worker(interval): n = 5 #進程數 while n>0: print("The time is :{0}".format(time.ctime())) #初始化時間 time.sleep(interval) #睡眠時間 n-=1 if __name__ == "__main__": # 創建進程,target:調用對象,args:傳參數到對象 p = multiprocessing.Process(target=worker,args=(2,)) p.start() #開啟進程 print("進程號:",p.pid) print("進程別名:",p.name) print("進程存活狀態:",p.is_alive())
運行結果:
進程號: 6784
進程別名: Process-1
進程存活狀態: True
The time is :Wed Nov 1 10:59:03 2017
The time is :Wed Nov 1 10:59:05 2017
The time is :Wed Nov 1 10:59:07 2017
The time is :Wed Nov 1 10:59:09 2017
The time is :Wed Nov 1 10:59:11 2017
(2)創建函數作為多進程
#!/usr/bin/env python # -*- coding:utf-8 -*- # Author:ZhengzhengLiu import multiprocessing import time #創建函數作為多進程 def work1(interval): print("work1...") time.sleep(interval) print("end work1...") def work2(interval): print("work2...") time.sleep(interval) print("end work2...") def work3(interval): print("work3...") time.sleep(interval) print("end work3...") if __name__ == "__main__": p1 = multiprocessing.Process(target=work1,args=(1,)) p2 = multiprocessing.Process(target=work2,args=(2,)) p3 = multiprocessing.Process(target=work3,args=(3,)) p1.start() p2.start() p3.start() print("The number of CPU is %d:"%(multiprocessing.cpu_count())) #打印CPU核數 for p in multiprocessing.active_children(): #循環打印子進程的名稱和pid print("子進程名稱:%s,子進程pid:%d" %(p.name,p.pid)) print("ending....")
運行結果:
The number of CPU is 4:
子進程名稱:Process-2,子進程pid:7108
子進程名稱:Process-1,子進程pid:1896
子進程名稱:Process-3,子進程pid:7952
ending....
work3...
work1...
work2...
end work1...
end work2...
end work3...
注:先運行主進程的內容,再運行子進程
(3)將進程定義成一個類
#!/usr/bin/env python # -*- coding:utf-8 -*- # Author:ZhengzhengLiu import multiprocessing import time #將進程定義為一個類 class ClockProcess(multiprocessing.Process): def __init__(self,interval): multiprocessing.Process.__init__(self) #重構了Process類里面的構造函數 self.interval = interval def run(self): #固定用run方法,啟動進程自動調用run方法 n = 5 while n>0: print("The time is {0}".format(time.ctime())) time.sleep(self.interval) n-=1 if __name__ == "__main__": p = ClockProcess(2) p.start()
運行結果:
The time is Wed Nov 1 11:31:28 2017
The time is Wed Nov 1 11:31:30 2017
The time is Wed Nov 1 11:31:32 2017
The time is Wed Nov 1 11:31:34 2017
The time is Wed Nov 1 11:31:36 2017
(4)Queue(隊列)實現多進程數據傳輸
#!/usr/bin/env python # -*- coding:utf-8 -*- # Author:ZhengzhengLiu import multiprocessing #Queue是多進程安全的隊列,可以使用實現多進程之間的數據傳遞 def writer_proc(q): try: q.put(1,block=False) #put方法插入數據到隊列中 except: pass def reader_proc(q): try: print(q.get(block=False)) #get方法從隊列中讀取并刪除一個元素 except: pass if __name__ == "__main__": q = multiprocessing.Queue() writer = multiprocessing.Process(target=writer_proc,args=(q,)) writer.start() reader = multiprocessing.Process(target=reader_proc,args=(q,)) reader.start() reader.join() writer.join()
運行結果:
1
關于如何在Python3.5中使用多進程就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。