程序同步
程序的資料是獨立存在的,程序也能加鎖。
from multiprocessing import process, lockdef f(l,i):
l.acquire()
print('hello world',i)
l.release()
if __name__ =='__main__':
lock = lock() #獲得鎖的例項
for i in range(10):
process(target=f,args=(lock,i)).start() #啟動程序,並且把鎖的例項傳到程序
執行結果
hello world 0hello world 2
hello world 1
hello world 3
hello world 6
hello world 7
hello world 8
hello world 4
hello world 5
hello world 9
程序為什麼要加鎖?
因為程序的資料是獨立存在的,並不會共享同一塊資料。但是有些資源是共享的,比如顯示器。如果每個程序都要輸出內容,那麼顯示的就很亂了,這個鎖就是在某個程序獨自輸出的時候獨佔,不會被其它程序干擾。
from multiprocessing import process, pool,freeze_supportimport time
import os
def foo(i):
time.sleep(2)
print('當前程序',os.getpid())
return i + 100
def bar(arg):
print("-->exec done:",arg)
if __name__ =='__main__':
freeze_support()
pool = pool(processes=5) #允許程序池中同時放入5個程序
for i in range(10):
print('end')
pool.close()
pool.join() #程序池中程序執行完畢後在關閉,如果注釋,那麼程式直接關閉。.join()
執行結果
當前程序 5816 #sleep 2s 列印當前程序 8124 #sleep 2s 列印
當前程序 6488 #sleep 2s 列印
當前程序 5356
當前程序 7036
當前程序 5816
當前程序 8124
當前程序 6488
當前程序 5356
當前程序 7036
end
以上是同步執行,程式顯示的效果是序列化執行。
並行化
from multiprocessing import process, pool,freeze_supportimport time
import os
def foo(i):
time.sleep(2)
print('當前程序',os.getpid())
return i + 100
def bar(arg):
print("-->exec done:",arg)
if __name__ =='__main__':
freeze_support()
pool = pool(processes=5) #允許程序池中同時放入5個程序
for i in range(10):
print('end')
pool.close()
pool.join() #程序池中程序執行完畢後在關閉,如果注釋,那麼程式直接關閉。.join()
執行結果
end當前程序 6060 #一次列印5個
當前程序 6952
-->exec done: 100
-->exec done: 101
當前程序 3388
-->exec done: 102
當前程序 1600
-->exec done: 103
當前程序 7648
-->exec done: 104
當前程序 6060
當前程序 6952
-->exec done: 105
-->exec done: 106
當前程序 3388
-->exec done: 107
當前程序 1600
-->exec done: 108
當前程序 7648
-->exec done: 109
callback() **函式,子程序執行完func,之後在呼叫的函式。 那麼這個函式是子程序呼叫的還是主程序呼叫的?
from multiprocessing import process, pool,freeze_supportimport time
import os
def foo(i):
time.sleep(2)
print('當前程序',os.getpid())
return i + 100
def bar(arg):
print("-->exec done:",arg,os.getpid()) #顯示呼叫當前函式的程序id
if __name__ =='__main__':
freeze_support()
pool = pool(processes=5) #允許程序池中同時放入5個程序
print("主程序",os.getpid()) #顯示主程序id
for i in range(10):
print('end')
pool.close()
pool.join() #程序池中程序執行完畢後在關閉,如果注釋,那麼程式直接關閉。.join()
執行結果
主程序 7052end當前程序 7992
當前程序 1848
-->exec done: 101 7052
-->exec done: 100 7052
當前程序 2212
-->exec done: 102 7052
當前程序 980
當前程序 8064
-->exec done: 103 7052
-->exec done: 104 7052
當前程序 7992
-->exec done: 105 7052
當前程序 1848
-->exec done: 106 7052
當前程序 2212
-->exec done: 107 7052
當前程序 8064
當前程序 980
-->exec done: 109 7052
-->exec done: 108 7052
這裡可以看到是主程序呼叫的**,這些寫的優點是,比如子程序做了資料備份要寫到資料庫,如果每個子程序都在執行的函式裡面寫,那麼每個程序都要連線一次資料庫,用主程序呼叫的方式就是可以省去這麼多的連線資料庫的操作。效率更高。
python 程序 Python程序池
當需要建立的子程序數量不多時,可以直接利用multiprocessing中的process動態成生多個程序,但如果是上百甚至上千個目標,手動的去建立程序的工作量巨大,此時就可以用到multiprocessing模組提供的pool方法。初始化pool時,可以指定乙個最大程序數,當有新的請求提交到poo...
Python 程序,程序池
注意 程序池中的程序如果發生異常時,主程序中並不會得到異常提示。如果程序池中的程序要使用佇列 queue 實現程序間通訊,必須使用queue multiprocessing.manager queue 的方式建立佇列 需要先導入multiprocessing模組 demo.py 程序池 coding...
Python的程序池
了解了python的多程序之後,我們會發現,如果有大量任務需要使用多程序來完成,則可能需要頻繁的建立刪除程序,給計算機帶來較多的資源消耗。多程序的缺點就是會頻繁的建立和刪除程序,消耗計算機資源 基於這種情況,於是出現了程序池。程序池的原理 舉乙個通俗易懂的例子 使用程序池步驟 建立程序池,在池內放入...