亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術(shù)文章
文章詳情頁

Python之多進(jìn)程與多線程的使用

瀏覽:87日期:2022-06-27 11:24:20
進(jìn)程與線程

想象在學(xué)校的一個機(jī)房,有固定數(shù)量的電腦,老師安排了一個爬蟲任務(wù)讓大家一起完成,每個學(xué)生使用一臺電腦爬取部分?jǐn)?shù)據(jù),將數(shù)據(jù)放到一個公共數(shù)據(jù)庫。共同資源就像公共數(shù)據(jù)庫,進(jìn)程就像每一個學(xué)生,每多一個學(xué)生,就多一個進(jìn)程來完成這個任務(wù),機(jī)房里的電腦數(shù)量就像CPU,所以進(jìn)程數(shù)量是CPU決定的,線程就像學(xué)生用一臺電腦開多個爬蟲,爬蟲數(shù)量由每臺電腦的運行內(nèi)存決定。一個CPU可以有多個進(jìn)程,一個進(jìn)程有一個或多個線程。

多進(jìn)程

1、導(dǎo)包

from multiprocessing import Process

2、寫兩個任務(wù)也就是兩個函數(shù)

3、創(chuàng)建一個進(jìn)程進(jìn)程名字 = Process(target=函數(shù)名字,函數(shù)參數(shù)傳字典或元組,是否守護(hù)進(jìn)程)

4、啟動進(jìn)程進(jìn)程名字.start()

5、是否開啟進(jìn)程守護(hù),一般主進(jìn)程會等待子進(jìn)程執(zhí)行完畢后再關(guān)閉程序。當(dāng)我們想程序主進(jìn)程跑完,直接銷毀掉未完成的子進(jìn)程,關(guān)閉程序的話,加上一句代碼 :1.創(chuàng)建進(jìn)程的時候傳參數(shù)daemon=True2.進(jìn)程名字.daemon=True

6、進(jìn)程編號導(dǎo)包os獲取當(dāng)前進(jìn)程編號

os.getpid()

獲取當(dāng)前父進(jìn)程的編號

os.getppid()

代碼示例(未開啟進(jìn)程守護(hù))

from multiprocessing import Processimport timeimport os# 一個寫作業(yè)函數(shù)def homeWork(name, count): for i in range(count): # 打印當(dāng)前進(jìn)程編號os.getpid() print('當(dāng)前進(jìn)程編號:', os.getpid()) # 打印當(dāng)前父進(jìn)程編號os.getppid() print('當(dāng)前父進(jìn)程編號:', os.getppid()) print(name, '正在寫作業(yè)...') time.sleep(0.2)# 一個打游戲函數(shù)def game(name, count): for i in range(count): # 打印當(dāng)前進(jìn)程編號os.getpid() print('當(dāng)前進(jìn)程編號:', os.getpid()) # 打印當(dāng)前父進(jìn)程編號os.getppid() print('當(dāng)前父進(jìn)程編號:', os.getppid()) print(name, '正在打游戲...') time.sleep(0.2)if __name__ == ’__main__’: # 打印當(dāng)前進(jìn)程編號os.getpid() print('當(dāng)前進(jìn)程編號:', os.getpid()) # 進(jìn)程1寫作業(yè) 元組傳參 p1 = Process(target=homeWork, args=('進(jìn)程1', 10)) # 進(jìn)程2打游戲 字典傳參 p2 = Process(target=game, kwargs={'name': '進(jìn)程2', 'count': 10}) # 啟動進(jìn)程 p1.start() p2.start() time.sleep(1) print('主進(jìn)程結(jié)束---------------------------------------------')

未開啟線程守護(hù)的運行結(jié)果:

# 可以看到主進(jìn)程結(jié)束的,其子進(jìn)程還在繼續(xù)當(dāng)前進(jìn)程編號: 14972當(dāng)前進(jìn)程編號: 5732當(dāng)前父進(jìn)程編號: 14972進(jìn)程1 正在寫作業(yè)...當(dāng)前進(jìn)程編號: 14752當(dāng)前父進(jìn)程編號: 14972進(jìn)程2 正在打游戲...當(dāng)前進(jìn)程編號: 5732當(dāng)前父進(jìn)程編號: 14972進(jìn)程1 正在寫作業(yè)...當(dāng)前進(jìn)程編號: 14752當(dāng)前父進(jìn)程編號: 14972進(jìn)程2 正在打游戲...當(dāng)前進(jìn)程編號: 5732當(dāng)前父進(jìn)程編號: 14972進(jìn)程1 正在寫作業(yè)...當(dāng)前進(jìn)程編號: 14752當(dāng)前父進(jìn)程編號: 14972進(jìn)程2 正在打游戲...當(dāng)前進(jìn)程編號: 5732當(dāng)前父進(jìn)程編號: 14972進(jìn)程1 正在寫作業(yè)...當(dāng)前進(jìn)程編號: 14752當(dāng)前父進(jìn)程編號: 14972進(jìn)程2 正在打游戲...主進(jìn)程結(jié)束---------------------------------------------當(dāng)前進(jìn)程編號: 5732當(dāng)前父進(jìn)程編號: 14972進(jìn)程1 正在寫作業(yè)...當(dāng)前進(jìn)程編號: 14752當(dāng)前父進(jìn)程編號: 14972進(jìn)程2 正在打游戲...當(dāng)前進(jìn)程編號: 5732當(dāng)前父進(jìn)程編號: 14972進(jìn)程1 正在寫作業(yè)...當(dāng)前進(jìn)程編號: 14752當(dāng)前父進(jìn)程編號: 14972進(jìn)程2 正在打游戲...當(dāng)前進(jìn)程編號: 5732當(dāng)前父進(jìn)程編號: 14972進(jìn)程1 正在寫作業(yè)...當(dāng)前進(jìn)程編號: 14752當(dāng)前父進(jìn)程編號: 14972進(jìn)程2 正在打游戲...當(dāng)前進(jìn)程編號: 5732當(dāng)前父進(jìn)程編號: 14972進(jìn)程1 正在寫作業(yè)...當(dāng)前進(jìn)程編號: 14752當(dāng)前父進(jìn)程編號: 14972進(jìn)程2 正在打游戲...當(dāng)前進(jìn)程編號: 5732當(dāng)前父進(jìn)程編號: 14972進(jìn)程1 正在寫作業(yè)...當(dāng)前進(jìn)程編號: 14752當(dāng)前父進(jìn)程編號: 14972進(jìn)程2 正在打游戲...當(dāng)前進(jìn)程編號: 5732當(dāng)前父進(jìn)程編號: 14972進(jìn)程1 正在寫作業(yè)...當(dāng)前進(jìn)程編號: 14752當(dāng)前父進(jìn)程編號: 14972進(jìn)程2 正在打游戲...

Process finished with exit code 0

代碼示例(開啟進(jìn)程守護(hù))

from multiprocessing import Processimport timeimport os# 一個寫作業(yè)函數(shù)def homeWork(name, count): for i in range(count): # 打印當(dāng)前進(jìn)程編號os.getpid() print('當(dāng)前進(jìn)程編號:', os.getpid()) # 打印當(dāng)前父進(jìn)程編號os.getppid() print('當(dāng)前父進(jìn)程編號:', os.getppid()) print(name, '正在寫作業(yè)...') time.sleep(0.2)# 一個打游戲函數(shù)def game(name, count): for i in range(count): # 打印當(dāng)前進(jìn)程編號os.getpid() print('當(dāng)前進(jìn)程編號:', os.getpid()) # 打印當(dāng)前父進(jìn)程編號os.getppid() print('當(dāng)前父進(jìn)程編號:', os.getppid()) print(name, '正在打游戲...') time.sleep(0.2)if __name__ == ’__main__’: # 打印當(dāng)前進(jìn)程編號os.getpid() print('當(dāng)前進(jìn)程編號:', os.getpid()) # 進(jìn)程1寫作業(yè) 元組傳參 第一種方法啟動進(jìn)程守護(hù) p1 = Process(target=homeWork, args=('進(jìn)程1', 10), daemon=True) # 進(jìn)程2打游戲 字典傳參 p2 = Process(target=game, kwargs={'name': '進(jìn)程2', 'count': 10}) # 第二種 p2.daemon = True # 啟動進(jìn)程 p1.start() p2.start() time.sleep(1) print('主進(jìn)程---------------------------------------------')

開啟進(jìn)程守護(hù)的運行結(jié)果

當(dāng)前進(jìn)程編號: 372當(dāng)前進(jìn)程編號: 10116當(dāng)前進(jìn)程編號: 9860當(dāng)前父進(jìn)程編號: 372進(jìn)程1 正在寫作業(yè)...當(dāng)前父進(jìn)程編號: 372進(jìn)程2 正在打游戲...當(dāng)前進(jìn)程編號: 9860當(dāng)前進(jìn)程編號: 10116當(dāng)前父進(jìn)程編號: 372進(jìn)程2 正在打游戲...當(dāng)前父進(jìn)程編號: 372進(jìn)程1 正在寫作業(yè)...當(dāng)前進(jìn)程編號: 9860當(dāng)前進(jìn)程編號: 10116當(dāng)前父進(jìn)程編號: 372進(jìn)程1 正在寫作業(yè)...當(dāng)前父進(jìn)程編號: 372進(jìn)程2 正在打游戲...當(dāng)前進(jìn)程編號: 9860當(dāng)前進(jìn)程編號: 10116當(dāng)前父進(jìn)程編號: 372進(jìn)程1 正在寫作業(yè)...當(dāng)前父進(jìn)程編號: 372進(jìn)程2 正在打游戲...主進(jìn)程結(jié)束---------------------------------------------

Process finished with exit code 0

多線程

1、導(dǎo)包

import threading

2、寫兩個任務(wù)也就是兩個函數(shù)

3、創(chuàng)建一個線程線程名字 = threading.Thread(target=函數(shù)名字,函數(shù)參數(shù)傳字典或元組,是否守護(hù)進(jìn)程)

4、啟動線程線程名字.start()

5、是否開啟線程守護(hù),一般當(dāng)前程序會等待子線程執(zhí)行完畢后再關(guān)閉程序。當(dāng)我們想程序跑完,銷毀掉未完成的子線程,直接關(guān)閉程序的話,加上一句代碼 :1.創(chuàng)建線程的時候傳參數(shù)daemon=True2.線程名字.daemon=True

6、線程編號獲取當(dāng)前線程編號

threading.current_thread()

代碼示例(未開啟進(jìn)程守護(hù))

import threadingimport time# 一個寫作業(yè)函數(shù)def homeWork(name, count): for i in range(count): # 打印當(dāng)前線程 print(threading.current_thread()) print(name, '正在寫作業(yè)...') time.sleep(0.2)# 一個打游戲函數(shù)def game(name, count): for i in range(count): # 打印當(dāng)前線程 print(threading.current_thread()) print(name, '正在打游戲...') time.sleep(0.2)if __name__ == ’__main__’: # 線程1寫作業(yè) 元組傳參 t1 = threading.Thread(target=homeWork, args=('進(jìn)程1', 10)) # 線程2打游戲 字典傳參 t2 = threading.Thread(target=game, kwargs={'name': '進(jìn)程2', 'count': 10}) # 啟動進(jìn)程 t1.start() t2.start() time.sleep(1) print('主進(jìn)程結(jié)束###################################################################################')

未開啟線程守護(hù)的運行結(jié)果

# 可以看到主進(jìn)程結(jié)束的,其線程還在繼續(xù)<Thread(Thread-1, started 3364)>進(jìn)程1 正在寫作業(yè)...<Thread(Thread-2, started 9100)>進(jìn)程2 正在打游戲...<Thread(Thread-2, started 9100)>進(jìn)程2 正在打游戲...<Thread(Thread-1, started 3364)>進(jìn)程1 正在寫作業(yè)...<Thread(Thread-1, started 3364)>進(jìn)程1 正在寫作業(yè)...<Thread(Thread-2, started 9100)>進(jìn)程2 正在打游戲...<Thread(Thread-2, started 9100)>進(jìn)程2 正在打游戲...<Thread(Thread-1, started 3364)>進(jìn)程1 正在寫作業(yè)...<Thread(Thread-1, started 3364)>進(jìn)程1 正在寫作業(yè)...<Thread(Thread-2, started 9100)>進(jìn)程2 正在打游戲...主進(jìn)程結(jié)束###################################################################################<Thread(Thread-2, started 9100)>進(jìn)程2 正在打游戲...<Thread(Thread-1, started 3364)>進(jìn)程1 正在寫作業(yè)...<Thread(Thread-1, started 3364)><Thread(Thread-2, started 9100)>進(jìn)程2 正在打游戲...進(jìn)程1 正在寫作業(yè)...<Thread(Thread-1, started 3364)>進(jìn)程1 正在寫作業(yè)...<Thread(Thread-2, started 9100)>進(jìn)程2 正在打游戲...<Thread(Thread-2, started 9100)><Thread(Thread-1, started 3364)>進(jìn)程1 進(jìn)程2正在寫作業(yè)... 正在打游戲...<Thread(Thread-2, started 9100)><Thread(Thread-1, started 3364)>

進(jìn)程2 進(jìn)程1 正在打游戲...正在寫作業(yè)...

Process finished with exit code 0

代碼示例(開啟線程守護(hù))

import threadingimport time# 一個寫作業(yè)函數(shù)def homeWork(name, count): for i in range(count): # 打印當(dāng)前線程 print(threading.current_thread()) print(name, '正在寫作業(yè)...') time.sleep(0.2)# 一個打游戲函數(shù)def game(name, count): for i in range(count): # 打印當(dāng)前線程 print(threading.current_thread()) print(name, '正在打游戲...') time.sleep(0.2)if __name__ == ’__main__’: # 線程1寫作業(yè) 元組傳參 t1 = threading.Thread(target=homeWork, args=('進(jìn)程1', 10), daemon=True) # 線程2打游戲 字典傳參 t2 = threading.Thread(target=game, kwargs={'name': '進(jìn)程2', 'count': 10}) t2.daemon = True # 啟動進(jìn)程 t1.start() t2.start() time.sleep(1) print('主進(jìn)程結(jié)束###################################################################################')

開啟線程守護(hù)的運行結(jié)果

<Thread(Thread-1, started daemon 15480)>進(jìn)程1 正在寫作業(yè)...<Thread(Thread-2, started daemon 13700)>進(jìn)程2 正在打游戲...<Thread(Thread-2, started daemon 13700)>進(jìn)程2 正在打游戲...<Thread(Thread-1, started daemon 15480)>進(jìn)程1 正在寫作業(yè)...<Thread(Thread-1, started daemon 15480)><Thread(Thread-2, started daemon 13700)>進(jìn)程1 進(jìn)程2 正在寫作業(yè)...正在打游戲...

<Thread(Thread-2, started daemon 13700)><Thread(Thread-1, started daemon 15480)>

進(jìn)程1進(jìn)程2 正在寫作業(yè)... 正在打游戲...

<Thread(Thread-1, started daemon 15480)>進(jìn)程1 正在寫作業(yè)...<Thread(Thread-2, started daemon 13700)>進(jìn)程2 正在打游戲...主進(jìn)程結(jié)束###################################################################################

Process finished with exit code 0

到此這篇關(guān)于Python之多進(jìn)程與多線程的使用的文章就介紹到這了,更多相關(guān)Python 多進(jìn)程與多線程內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 久久亚洲网 | 亚洲情a成黄在线观看 | 欧美大黄| 欧美日韩精品一区二区三区视频 | 久久精品免看国产成 | 青青久操| 美女啪啪国产 | 色综合天天综合中文网 | 国产日韩欧美在线视频免费观看 | 成年男女男免费视频网站不卡 | 国产成人高清亚洲一区久久 | 欧美三级不卡在线观线看高清 | 国产免费人做爰午夜视频 | 国产一区二区日韩欧美在线 | 中文字幕曰韩一区二区不卡 | 国产一区二区不卡免费观在线 | 99视频在线看 | 麻豆 一区 精品 在线 | 亚洲精品一区二区久久 | 国产区1| 三级毛片网 | 日韩经典第一页 | 免费一级网站免费 | 亚洲无圣光一区二区 | 久久精品韩国日本国产 | 国产一级簧片 | 一级黄色片网 | 秀人网福利视频在线观看 | 1000部国产成人免费视频 | 麻豆传媒2021精品传媒一区 | 欧美不在线 | 久久久久国产一级毛片高清板 | 国产精品免费综合一区视频 | 五月天婷婷综合网 | 国产成人毛片视频不卡在线 | 一区二区在线免费视频 | 美女黄色一级片 | 日韩乱淫 | 国外精品视频在线观看免费 | 国产91精品高清一区二区三区 | 日本一道免费一区二区三区 |