Python實(shí)現(xiàn)多進(jìn)程的四種方式
方式一: os.fork()
# -*- coding:utf-8 -*- """ pid=os.fork() 1.只用在Unix系統(tǒng)中有效,Windows系統(tǒng)中無(wú)效 2.fork函數(shù)調(diào)用一次,返回兩次:在父進(jìn)程中返回值為子進(jìn)程id,在子進(jìn)程中返回值為0 """ import os pid=os.fork() if pid==0: print("執(zhí)行子進(jìn)程,子進(jìn)程pid={pid},父進(jìn)程ppid={ppid}".format(pid=os.getpid(),ppid=os.getppid())) else: print("執(zhí)行父進(jìn)程,子進(jìn)程pid={pid},父進(jìn)程ppid={ppid}".format(pid=pid,ppid=os.getpid()))
方式二: 使用multiprocessing模塊: 創(chuàng)建Process的實(shí)例,傳入任務(wù)執(zhí)行函數(shù)作為參數(shù)
# -*- coding:utf-8 -*- """ Process常用屬性與方法: name:進(jìn)程名 pid:進(jìn)程id run(),自定義子類(lèi)時(shí)覆寫(xiě) start(),開(kāi)啟進(jìn)程 join(timeout=None),阻塞進(jìn)程 terminate(),終止進(jìn)程 is_alive(),判斷進(jìn)程是否存活 """ import os,time from multiprocessing import Process def worker(): print("子進(jìn)程執(zhí)行中>>> pid={0},ppid={1}".format(os.getpid(),os.getppid())) time.sleep(2) print("子進(jìn)程終止>>> pid={0}".format(os.getpid())) def main(): print("主進(jìn)程執(zhí)行中>>> pid={0}".format(os.getpid())) ps=[] # 創(chuàng)建子進(jìn)程實(shí)例 for i in range(2): p=Process(target=worker,name="worker"+str(i),args=()) ps.append(p) # 開(kāi)啟進(jìn)程 for i in range(2): ps[i].start() # 阻塞進(jìn)程 for i in range(2): ps[i].join() print("主進(jìn)程終止") if __name__ == '__main__': main()
方式三: 使用multiprocessing模塊: 派生Process的子類(lèi),重寫(xiě)run方法
# -*- coding:utf-8 -*- import os,time from multiprocessing import Process class MyProcess(Process): def __init__(self): Process.__init__(self) def run(self): print("子進(jìn)程開(kāi)始>>> pid={0},ppid={1}".format(os.getpid(),os.getppid())) time.sleep(2) print("子進(jìn)程終止>>> pid={}".format(os.getpid())) def main(): print("主進(jìn)程開(kāi)始>>> pid={}".format(os.getpid())) myp=MyProcess() myp.start() # myp.join() print("主進(jìn)程終止") if __name__ == '__main__': main()
方式四: 使用進(jìn)程池Pool
# -*- coding:utf-8 -*- import os,time from multiprocessing import Pool def worker(arg): print("子進(jìn)程開(kāi)始執(zhí)行>>> pid={},ppid={},編號(hào){}".format(os.getpid(),os.getppid(),arg)) time.sleep(0.5) print("子進(jìn)程終止>>> pid={},ppid={},編號(hào){}".format(os.getpid(),os.getppid(),arg)) def main(): print("主進(jìn)程開(kāi)始執(zhí)行>>> pid={}".format(os.getpid())) ps=Pool(5) for i in range(10): # ps.apply(worker,args=(i,)) # 同步執(zhí)行 ps.apply_async(worker,args=(i,)) # 異步執(zhí)行 # 關(guān)閉進(jìn)程池,停止接受其它進(jìn)程 ps.close() # 阻塞進(jìn)程 ps.join() print("主進(jìn)程終止") if __name__ == '__main__': main()
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接
相關(guān)文章
Python抓取通過(guò)Ajax加載數(shù)據(jù)的示例
在網(wǎng)頁(yè)上,有一些內(nèi)容是通過(guò)執(zhí)行Ajax請(qǐng)求動(dòng)態(tài)加載數(shù)據(jù)渲染出來(lái)的,本文主要介紹了使用Python抓取通過(guò)Ajax加載數(shù)據(jù),感興趣的可以了解一下2023-05-05django實(shí)現(xiàn)將修改好的新模型寫(xiě)入數(shù)據(jù)庫(kù)
這篇文章主要介紹了django實(shí)現(xiàn)將修改好的新模型寫(xiě)入數(shù)據(jù)庫(kù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03Matlab中的mat數(shù)據(jù)轉(zhuǎn)成python中使用的npy數(shù)據(jù)遇到的坑及解決
這篇文章主要介紹了Matlab中的mat數(shù)據(jù)轉(zhuǎn)成python中使用的npy數(shù)據(jù)遇到的坑及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12解決安裝pycharm后不能執(zhí)行python腳本的問(wèn)題
今天小編就為大家分享一篇解決安裝pycharm后不能執(zhí)行python腳本的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-01-01python 實(shí)現(xiàn)百度網(wǎng)盤(pán)非會(huì)員上傳超過(guò)500個(gè)文件的方法
這篇文章主要介紹了python 實(shí)現(xiàn)百度網(wǎng)盤(pán)非會(huì)員上傳超過(guò)500個(gè)文件的方法,幫助大家更好的利用python解決問(wèn)題,感興趣的朋友可以了解下2021-01-01Google開(kāi)源的Python格式化工具YAPF的安裝和使用教程
Google的開(kāi)發(fā)者文檔中有一套Python的代碼書(shū)寫(xiě)規(guī)范,而在GitHub上同樣開(kāi)源了一款名為YAPF的命令行程序用作Python的格式化,下面我們就來(lái)看下這款Google開(kāi)源的Python格式化工具YAPF的安裝和使用教程2016-05-05keras slice layer 層實(shí)現(xiàn)方式
這篇文章主要介紹了keras slice layer 層實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-06-06