Python進程Multiprocessing模塊原理解析
先看看下面的幾個方法:
star() 方法啟動進程, join() 方法實現進程間的同步,等待所有進程退出。 close() 用來阻止多余的進程涌入進程池 Pool 造成進程阻塞。參數:
target 是函數名字,需要調用的函數
args 函數需要的參數,以 tuple 的形式傳入
用法:
multiprocessing.Process(group=None, target=None, name=None, args=(), kwargs={}, *, daemon=None)
寫一個的例子:
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())) # 默認為自己電腦的核數 p = Pool(2) for i in range(5): p.apply_async(pr,args=(’xdxd’,)) p.close() p.join() print('----------close-----------------')
通過結果可以看出,是2個進程同時啟動,同時啟動的進程數與pool中設置的數量和自己電腦的核數有關
結果:
-------------------------------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-----------------
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。
相關文章:
1. jsp網頁實現貪吃蛇小游戲2. jsp+servlet簡單實現上傳文件功能(保存目錄改進)3. JavaScript實現組件化和模塊化方法詳解4. ASP.NET MVC遍歷驗證ModelState的錯誤信息5. HTML5 Canvas繪制圖形從入門到精通6. .Net Core和RabbitMQ限制循環消費的方法7. 淺談SpringMVC jsp前臺獲取參數的方式 EL表達式8. SpringMVC+Jquery實現Ajax功能9. ASP中if語句、select 、while循環的使用方法10. asp(vbs)Rs.Open和Conn.Execute的詳解和區別及&H0001的說明
