Python3.5多進(jìn)程原理與用法實(shí)例分析
本文實(shí)例講述了Python3.5多進(jìn)程原理與用法。分享給大家供大家參考,具體如下:

進(jìn)程類:Process

示例及代碼:

(1)創(chuàng)建函數(shù)作為單進(jìn)程
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:ZhengzhengLiu
import multiprocessing
import time
#創(chuàng)建函數(shù)并將其作為單個(gè)進(jìn)程
def worker(interval):
n = 5 #進(jìn)程數(shù)
while n>0:
print("The time is :{0}".format(time.ctime())) #初始化時(shí)間
time.sleep(interval) #睡眠時(shí)間
n-=1
if __name__ == "__main__":
# 創(chuàng)建進(jìn)程,target:調(diào)用對(duì)象,args:傳參數(shù)到對(duì)象
p = multiprocessing.Process(target=worker,args=(2,))
p.start() #開啟進(jìn)程
print("進(jìn)程號(hào):",p.pid)
print("進(jìn)程別名:",p.name)
print("進(jìn)程存活狀態(tài):",p.is_alive())
運(yùn)行結(jié)果:
進(jìn)程號(hào): 6784
進(jìn)程別名: Process-1
進(jìn)程存活狀態(tài): 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)創(chuàng)建函數(shù)作為多進(jìn)程
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:ZhengzhengLiu
import multiprocessing
import time
#創(chuàng)建函數(shù)作為多進(jìn)程
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核數(shù)
for p in multiprocessing.active_children(): #循環(huán)打印子進(jìn)程的名稱和pid
print("子進(jìn)程名稱:%s,子進(jìn)程pid:%d" %(p.name,p.pid))
print("ending....")
運(yùn)行結(jié)果:
The number of CPU is 4:
子進(jìn)程名稱:Process-2,子進(jìn)程pid:7108
子進(jìn)程名稱:Process-1,子進(jìn)程pid:1896
子進(jìn)程名稱:Process-3,子進(jìn)程pid:7952
ending....
work3...
work1...
work2...
end work1...
end work2...
end work3...
注:先運(yùn)行主進(jìn)程的內(nèi)容,再運(yùn)行子進(jìn)程
(3)將進(jìn)程定義成一個(gè)類
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:ZhengzhengLiu
import multiprocessing
import time
#將進(jìn)程定義為一個(gè)類
class ClockProcess(multiprocessing.Process):
def __init__(self,interval):
multiprocessing.Process.__init__(self) #重構(gòu)了Process類里面的構(gòu)造函數(shù)
self.interval = interval
def run(self): #固定用run方法,啟動(dòng)進(jìn)程自動(dòng)調(diào)用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()
運(yùn)行結(jié)果:
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(隊(duì)列)實(shí)現(xiàn)多進(jìn)程數(shù)據(jù)傳輸
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:ZhengzhengLiu
import multiprocessing
#Queue是多進(jìn)程安全的隊(duì)列,可以使用實(shí)現(xiàn)多進(jìn)程之間的數(shù)據(jù)傳遞
def writer_proc(q):
try:
q.put(1,block=False) #put方法插入數(shù)據(jù)到隊(duì)列中
except:
pass
def reader_proc(q):
try:
print(q.get(block=False)) #get方法從隊(duì)列中讀取并刪除一個(gè)元素
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()
運(yùn)行結(jié)果:
1
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python進(jìn)程與線程操作技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進(jìn)階經(jīng)典教程》、《Python+MySQL數(shù)據(jù)庫(kù)程序設(shè)計(jì)入門教程》及《Python常見數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
解決python gdal投影坐標(biāo)系轉(zhuǎn)換的問題
今天小編就為大家分享一篇解決python gdal投影坐標(biāo)系轉(zhuǎn)換的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2020-01-01
Pytorch: 自定義網(wǎng)絡(luò)層實(shí)例
今天小編就為大家分享一篇Pytorch: 自定義網(wǎng)絡(luò)層實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2020-01-01
python定時(shí)檢測(cè)無(wú)響應(yīng)進(jìn)程并重啟的實(shí)例代碼
這篇文章主要介紹了python定時(shí)檢測(cè)無(wú)響應(yīng)進(jìn)程并重啟的實(shí)例代碼,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-04-04
pygame實(shí)現(xiàn)俄羅斯方塊游戲(AI篇2)
這篇文章主要為大家詳細(xì)介紹了pygame實(shí)現(xiàn)俄羅斯方塊游戲AI的第2篇,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-10-10
python成長(zhǎng)技能之正則表達(dá)式示例詳解
這篇文章主要介紹了python正則表達(dá)式的相關(guān)資料,涵蓋了正則表達(dá)式的基本語(yǔ)法、字符匹配、重復(fù)出現(xiàn)數(shù)量、字符集、邊界匹配、組、貪婪與非貪婪匹配等內(nèi)容,并通過實(shí)際例子展示了如何使用正則表達(dá)式進(jìn)行字符串匹配和處理,需要的朋友可以參考下2025-03-03
使用 Python 獲取 Linux 系統(tǒng)信息的代碼
在本文中,我們將會(huì)探索使用Python編程語(yǔ)言工具來(lái)檢索Linux系統(tǒng)各種信息,需要的朋友可以參考下2014-07-07
python使用PIL模塊實(shí)現(xiàn)給圖片打水印的方法
這篇文章主要介紹了python使用PIL模塊實(shí)現(xiàn)給圖片打水印的方法,涉及使用PIL模塊操作圖片的相關(guān)技巧,需要的朋友可以參考下2015-05-05
python 用pandas實(shí)現(xiàn)數(shù)據(jù)透視表功能
這篇文章主要介紹了python 用pandas實(shí)現(xiàn)數(shù)據(jù)透視表功能的方法,幫助大家更好的理解和使用python,感興趣的朋友可以了解下2020-12-12
Python實(shí)現(xiàn)圖片批量加入水印代碼實(shí)例
這篇文章主要介紹了Python實(shí)現(xiàn)圖片批量加入水印代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11

