Python進(jìn)程Multiprocessing模塊原理解析
先看看下面的幾個(gè)方法:
star() 方法啟動(dòng)進(jìn)程, join() 方法實(shí)現(xiàn)進(jìn)程間的同步,等待所有進(jìn)程退出。 close() 用來阻止多余的進(jìn)程涌入進(jìn)程池 Pool 造成進(jìn)程阻塞。參數(shù):
target 是函數(shù)名字,需要調(diào)用的函數(shù)
args 函數(shù)需要的參數(shù),以 tuple 的形式傳入
用法:
multiprocessing.Process(group=None, target=None, name=None, args=(), kwargs={}, *, daemon=None)
寫一個(gè)的例子:
from multiprocessing import Poolimport os,timedef pr(str): print('The ' + str + ' is %s' %(os.getpid())) time.sleep(1) print('The ' + str + ' is close')if __name__ == '__main__': print(’-------------------------------’) print('the current pid: '+ str(os.getpid())) # 默認(rèn)為自己電腦的核數(shù) p = Pool(2) for i in range(5): p.apply_async(pr,args=(’xdxd’,)) p.close() p.join() print('----------close-----------------')
通過結(jié)果可以看出,是2個(gè)進(jìn)程同時(shí)啟動(dòng),同時(shí)啟動(dòng)的進(jìn)程數(shù)與pool中設(shè)置的數(shù)量和自己電腦的核數(shù)有關(guān)
結(jié)果:
-------------------------------the current pid: 9562The xdxd is 9563The xdxd is 9564The xdxd is closeThe xdxd is closeThe xdxd is 9563The xdxd is 9564The xdxd is closeThe xdxd is closeThe xdxd is 9563The xdxd is close----------close-----------------
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. python自動(dòng)化調(diào)用百度api解決驗(yàn)證碼2. 解決Android Studio 格式化 Format代碼快捷鍵問題3. Java Bean與Map之間相互轉(zhuǎn)化的實(shí)現(xiàn)方法4. vue實(shí)現(xiàn)web在線聊天功能5. 完美解決vue 中多個(gè)echarts圖表自適應(yīng)的問題6. SpringBoot+TestNG單元測試的實(shí)現(xiàn)7. Python使用urlretrieve實(shí)現(xiàn)直接遠(yuǎn)程下載圖片的示例代碼8. Java使用Tesseract-Ocr識(shí)別數(shù)字9. 在Chrome DevTools中調(diào)試JavaScript的實(shí)現(xiàn)10. Springboot 全局日期格式化處理的實(shí)現(xiàn)
