由於python下呼叫linux的shell命令都需要等待返回,所以常常我們設定的多執行緒都達不到效果,
因此在呼叫shell命令不需要返回時,使用threading模組並不是最好的方法。
python提供了非常好用的多程序包multiprocessing,你只需要定義乙個函式,python會替你完成其他所有事情。
借助這個包,可以輕鬆完成從單程序到併發執行的轉換。
1、新建單一程序
如果我們新建少量程序,可以如下:
importmultiprocessing
import
time
def func(msg):
for i in xrange(3):
print msg
time.sleep(1)
if __name__ == "__main__":
p = multiprocessing.process(target=func, args=("hello", ))
p.start()
p.join()
print "sub-process done."
processes=4是最多併發程序數量。
importmultiprocessing
import
time
def func(msg):
for i in xrange(3):
print msg
time.sleep(1)
if __name__ == "__main__":
pool = multiprocessing.pool(processes=4)
for i in xrange(10):
msg = "hello %d" %(i)
pool.close()
pool.join()
print "sub-process(es) done."
3、使用pool,並需要關注結果
importmultiprocessing
import
time
def func(msg):
for i in xrange(3):
print msg
time.sleep(1)
return "done " +msg
if __name__ == "__main__":
pool = multiprocessing.pool(processes=4)
result =
for i in xrange(10):
msg = "hello %d" %(i)
pool.close()
pool.join()
forres in result:
print res.get()
print "sub-process(es) done."
multiprocessing.freeze_support()
附錄(自己的指令碼):
#!/usr/bin/pythonimport
threading
import
subprocess
import
datetime
import
multiprocessing
def dd_test(round, th):
test_file_arg = 'of=/zbkc/test_mds_crash/1m_%s_%s_{}' %(round, th)
command = "seq 100 | xargs -i dd if=/dev/zero %s bs=1m count=1" %test_file_arg
print command
subprocess.call(command,shell=true,stdout=open('/dev/null','w'),stderr=subprocess.stdout)
def mds_stat(round):
p = subprocess.popen("zbkc mds stat", shell = true, stdout =subprocess.pipe)
out =p.stdout.readlines()
if out[0].find('active') != -1:
command = "echo '0205pm %s round mds status ok, %s' >> /round_record" %(round, datetime.datetime.now())
command_2 = "time (ls /zbkc/test_mds_crash/) 2>>/round_record"command_3 = "ls /zbkc/test_mds_crash | wc -l >> /round_record"subprocess.call(command,shell=true)
subprocess.call(command_2,shell=true)
subprocess.call(command_3,shell=true)
return 1
else
: command = "echo '0205 %s round mds status abnormal, %s, %s' >> /round_record" %(round, out[0], datetime.datetime.now())
subprocess.call(command,shell=true)
return 0#threads =
for round in range(1, 1600):
pool = multiprocessing.pool(processes = 10) #使用程序池
for th in range(10):
# th_name = "thread-" +str(th)
# threading.thread(target = dd_test, args = (round, th), name =th_name).start() #建立多執行緒任務
pool.close()
pool.join()
#等待執行緒完成
#
fort in threads:
# t.join()
if mds_stat(round) == 0:
subprocess.call("zbkc -s",shell=true)
break
原文:
python併發之多程序
一 multiprocessing模組介紹 python中的多執行緒無法利用多核優勢,如果想要充分地使用多核cpu的資源 os.cpu count 檢視 在python中大部分情況需要使用多程序。python提供了multiprocessing。multiprocessing模組用來開啟子程序,並在...
python併發程式設計 多程序
import os import time from multiprocessing import process def func args,args2 print args,args2 time.sleep 3 print 子程序 os.getpid print 子程序的父程序 os.getpp...
python 多程序併發demo
下午需要簡單處理乙份資料,就直接隨手寫指令碼處理了,但發現效率太低,速度太慢,就改成多程序了 程式涉及計算 檔案讀寫,鑑於計算內容挺多的,就用多程序了 計算密集 import pandas as pd from pathlib import path from concurrent.futures ...