簡(jiǎn)單談?wù)刾ython中的多進(jìn)程
進(jìn)程是由系統(tǒng)自己管理的。
1:最基本的寫法
from multiprocessing import Pool def f(x): return x*x if __name__ == '__main__': p = Pool(5) print(p.map(f, [1, 2, 3])) [1, 4, 9]
2、實(shí)際上是通過os.fork的方法產(chǎn)生進(jìn)程的
unix中,所有進(jìn)程都是通過fork的方法產(chǎn)生的。
multiprocessing Process os info(title): title , __name__ (os, ): , os.getppid() , os.getpid() f(name): info() , name __name__ == : info() p = Process(=f, =(,)) p.start() p.join()
3、線程共享內(nèi)存
threading
run(info_list,n):
info_list.append(n)
info_list
__name__ == :
info=[]
i ():
p=threading.Thread(=run,=[info,i])
p.start()
[0]
[0, 1]
[0, 1, 2]
[0, 1, 2, 3]
[0, 1, 2, 3, 4]
[0, 1, 2, 3, 4, 5]
[0, 1, 2, 3, 4, 5, 6]
[0, 1, 2, 3, 4, 5, 6, 7]
[0, 1, 2, 3, 4, 5, 6, 7, 8]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
進(jìn)程不共享內(nèi)存:
multiprocessing Process
run(info_list,n):
info_list.append(n)
info_list
__name__ == :
info=[]
i ():
p=Process(=run,=[info,i])
p.start()
[1]
[2]
[3]
[0]
[4]
[5]
[6]
[7]
[8]
[9]
若想共享內(nèi)存,需使用multiprocessing模塊中的Queue
multiprocessing Process, Queue
f(q,n):
q.put([n,])
__name__ == :
q=Queue()
i ():
p=Process(=f,=(q,i))
p.start()
:
q.get()
4、鎖:僅是對(duì)于屏幕的共享,因?yàn)檫M(jìn)程是獨(dú)立的,所以對(duì)于多進(jìn)程沒有用
multiprocessing Process, Lock
f(l, i):
l.acquire()
, i
l.release()
__name__ == :
lock = Lock()
num ():
Process(=f, =(lock, num)).start()
hello world 0
hello world 1
hello world 2
hello world 3
hello world 4
hello world 5
hello world 6
hello world 7
hello world 8
hello world 9
5、進(jìn)程間內(nèi)存共享:Value,Array
multiprocessing Process, Value, Array
f(n, a):
n.value = i ((a)):
a[i] = -a[i]
__name__ == :
num = Value(, )
arr = Array(, ())
num.value
arr[:]
p = Process(=f, =(num, arr))
p.start()
p.join()
0.0
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
3.1415927
[0, -1, -2, -3, -4, -5, -6, -7, -8, -9]
#manager共享方法,但速度慢
multiprocessing Process, Manager
f(d, l):
d[] = d[] = d[] = l.reverse()
__name__ == :
manager = Manager()
d = manager.dict()
l = manager.list(())
p = Process(=f, =(d, l))
p.start()
p.join()
d
l
# print '-------------'這里只是另一種寫法
# print pool.map(f,range(10))
{0.25: None, 1: '1', '2': 2}
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
#異步:這種寫法用的不多
multiprocessing Pool
time
f(x):
x*x
time.sleep()
x*x
__name__ == :
pool=Pool(=)
res_list=[]
i ():
res=pool.apply_async(f,[i]) res_list.append(res)
r res_list:
r.get(timeout=10) #超時(shí)時(shí)間
同步的就是apply
相關(guān)文章
Python?Excel操作從零學(xué)習(xí)掌握openpyxl用法
這篇文章主要為大家介紹了Python?Excel操作從零學(xué)習(xí)掌握openpyxl用法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08
Python把excel文件數(shù)據(jù)轉(zhuǎn)化為字典格式存儲(chǔ)詳解
這篇文章主要介紹了Python把excel文件數(shù)據(jù)轉(zhuǎn)化為字典格式存儲(chǔ)詳解,在Python中有時(shí)候需要操作excel表格的數(shù)據(jù),把excel表格轉(zhuǎn)化為字典存起來,方便讀取,今天我們就來看看如何轉(zhuǎn)換,需要的朋友可以參考下2023-08-08
python使用requests庫(kù)提交multipart/form-data請(qǐng)求的方法詳解
multipart/form-data的基礎(chǔ)是post請(qǐng)求,即基于post請(qǐng)求來實(shí)現(xiàn)的 ,下面這篇文章主要給大家介紹了關(guān)于python使用requests庫(kù)提交multipart/form-data請(qǐng)求的相關(guān)資料,需要的朋友可以參考下2023-01-01
Django中URL視圖函數(shù)的一些高級(jí)概念介紹
這篇文章主要介紹了Django中URL視圖函數(shù)的一些高級(jí)概念,Django是Python重多人氣框架中最為著名的一個(gè),需要的朋友可以參考下2015-07-07
python 反編譯exe文件為py文件的實(shí)例代碼
這篇文章主要介紹了python 反編譯exe文件為py文件的實(shí)例代碼,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-06-06
Python爬蟲之Selenium實(shí)現(xiàn)窗口截圖
這篇文章主要介紹了Python爬蟲之Selenium實(shí)現(xiàn)窗口截圖,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12
Python使用ClickHouse的實(shí)踐與踩坑記錄
這篇文章主要介紹了Python使用ClickHouse的實(shí)踐與踩坑記錄,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-05-05
Python實(shí)現(xiàn)多線程下載腳本的示例代碼
這篇文章主要介紹了Python實(shí)現(xiàn)多線程下載腳本的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04

