python多進(jìn)程操作實(shí)例
由于CPython實(shí)現(xiàn)中的GIL的限制,python中的多線程其實(shí)并不是真正的多線程,如果想要充分地使用多核CPU的資源,在python中大部分情況我們需要使用多進(jìn)程。 這也許就是python中多進(jìn)程類庫如此簡潔好用的原因所在。在python中可以向多線程一樣簡單地使用多進(jìn)程。
一、多進(jìn)程
process的成員變量和方法:
>>class multiprocessing.Process([group[, target[, name[, args[, kwargs]]]]]) 來的定義類似于threading.Thread。target表示此進(jìn)程運(yùn)行的函數(shù),args和kwargs表示target的參數(shù)。
>>name, pid
分別表示進(jìn)程的名字,進(jìn)程id。
>> daemon成員
daemon標(biāo)志位bool變量,需要在start()調(diào)用前設(shè)置。daemon的初始值是從父進(jìn)程繼承而來。當(dāng)一個(gè)進(jìn)程結(jié)束的時(shí)候,它嘗試去結(jié)束它的所有的daemon子進(jìn)程。
注意:
daemon進(jìn)程不允許創(chuàng)建子進(jìn)程。否則當(dāng)daemon進(jìn)程結(jié)束的時(shí)候它的子進(jìn)程不能被結(jié)束。
這里的daemon不是Unix的daemon進(jìn)程,當(dāng)父進(jìn)程結(jié)束的時(shí)候所有的daemon子進(jìn)程也將被終止(對于非daemon進(jìn)程,父進(jìn)程不等待非daemon的紫子進(jìn)程,除非顯示地對非daemon子進(jìn)程使用join()方法)。
>> exitcode
如果進(jìn)程還沒有退出,則為None,如果正確的退出則為0,如果有錯(cuò)誤則為>0的錯(cuò)誤代碼,如果進(jìn)程為終止則為-1*singal。
>> start(), is_live(), terminate()
start()用來啟動(dòng)進(jìn)程,is_live()用來查看進(jìn)程的狀態(tài),terminate()用來終止進(jìn)程。
>> run()
可以在process的子類中重載run()方法,從而設(shè)定進(jìn)程的任務(wù)。重載process是構(gòu)造新進(jìn)程的另一種方式,一定程度上上等價(jià)于process的target參數(shù)。
multiprcessing的靜態(tài)方法:
>> multiprocessing.cpu_count()
用來獲得當(dāng)前的CPU的核數(shù),可以用來設(shè)置接下來子進(jìn)程的個(gè)數(shù)。
>> multiprocessing.active_children()
用來獲得當(dāng)前所有的子進(jìn)程,包括daemon和非daemon子進(jìn)程。
實(shí)例:
import multiprocessing
import time
import sys
def worker(num):
p = multiprocessing.current_process()
print ('Starting:' + p.name + ":" + str(p.pid))
print(str(num))
sys.stdout.flush()
print ('Exiting :' + p.name + ":" + str(p.pid))
sys.stdout.flush()
def daemon():
p = multiprocessing.current_process()
print ('Starting:' + p.name + ":" + str(p.pid))
sys.stdout.flush()
time.sleep(10)
print ('Exiting :' + p.name + ":" + str(p.pid))
sys.stdout.flush()
def non_daemon():
p = multiprocessing.current_process()
print ('Starting:' + p.name + ":" + str(p.pid))
sys.stdout.flush()
time.sleep(20)
print ('Exiting :' + p.name + ":" + str(p.pid))
sys.stdout.flush()
if __name__ == '__main__':
w = multiprocessing.Process(name='worker', target=worker, args=(100,))
d = multiprocessing.Process(name='daemon', target=daemon)
d.daemon = True
nd = multiprocessing.Process(name='non-daemon', target=non_daemon)
w.start()
d.start()
nd.start()
print("the number of CPU is " + str(multiprocessing.cpu_count()))
print("All children processes:")
for p in multiprocessing.active_children():
print("child:" + p.name + ":" + str(p.pid))
print()
w.join()
#d.join()
運(yùn)行結(jié)果:

可以從上面的例子看到?jīng)]有多非daemon子進(jìn)程使用join()方法,結(jié)果父進(jìn)程沒有等待非daemon進(jìn)程結(jié)束就退出了。
- python多進(jìn)程共享變量
- 探究Python多進(jìn)程編程下線程之間變量的共享問題
- Python 多進(jìn)程和數(shù)據(jù)傳遞的理解
- Python實(shí)現(xiàn) 多進(jìn)程導(dǎo)入CSV數(shù)據(jù)到 MySQL
- Python利用多進(jìn)程將大量數(shù)據(jù)放入有限內(nèi)存的教程
- Python多進(jìn)程并發(fā)(multiprocessing)用法實(shí)例詳解
- 淺析Python中的多進(jìn)程與多線程的使用
- Python多進(jìn)程同步Lock、Semaphore、Event實(shí)例
- python 多進(jìn)程通信模塊的簡單實(shí)現(xiàn)
- Python控制多進(jìn)程與多線程并發(fā)數(shù)總結(jié)
- Python實(shí)現(xiàn)多進(jìn)程共享數(shù)據(jù)的方法分析
相關(guān)文章
python腳本設(shè)置超時(shí)機(jī)制系統(tǒng)時(shí)間的方法
這篇文章主要介紹了python腳本設(shè)置超時(shí)機(jī)制系統(tǒng)時(shí)間的方法,感興趣的小伙伴們可以參考一下2016-02-02
Python安全獲取域管理員權(quán)限幾種方式操作示例
在不考慮直接攻擊域控的情況下,如何快速獲取域管理員權(quán)限呢?本文分享幾種常見的獲取域管理員權(quán)限的方式,有需要的朋友可以借鑒參考下2021-10-10
python批量導(dǎo)入數(shù)據(jù)進(jìn)Elasticsearch的實(shí)例
今天小編就為大家分享一篇python批量導(dǎo)入數(shù)據(jù)進(jìn)Elasticsearch的實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-05-05
Python之兩種模式的生產(chǎn)者消費(fèi)者模型詳解
今天小編就為大家分享一篇Python之兩種模式的生產(chǎn)者消費(fèi)者模型詳解,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-10-10

