一,背景
最近工作中需要使用python指令碼將乙個xlsx檔案中的資料全部匯入到es中,由於檔案中的內容豐富,數量又多。直接通過python檔案讀入之後在寫入es中,時間比較長,總共接近兩萬條資料從執行程式,到最終入到es完成,差不多要花10分鐘。
通過使用多程序的方式,極大地提高了資料匯入es的效能。在這裡記錄一下對python多程序程式進行的一些分析,研究。
二,效能消耗在哪些地方
三,程式功能說明為了優化匯入速度需要找到花費時間較長的地方在哪。一般來說,乙個任務執行時間較長會在兩個地方。
1.進行複雜的計算。
2.執行io操作中存在阻塞,長時間等待io。
該程式有幾個部分,首先是使用openpyxl讀xlsx檔案中的資料,然後放到乙個list中。其次,構造es的資料格式,匯入資料到es中。由此可以看出來,在這個程式中既包含了複雜計算比如向list中插入資料,又包含io操作,所以,在這兩個部分都存在耗費時間的問題,這就需要具體問題具體分析。
四,實驗環境
測試環境一:
系統:單核,單cpu
資料量:2000
操作:讀取xlsx,併發送到es中
首先在單核單cpu,單程序情況下:
其次在單核單cpu,多程序情況下:main 361 ...sat nov 11 04:32:49 2017
main 363...sat nov 11 04:33:07 2017
---------------
耗時為18s
測試環境二:main 361 ...sat nov 11 04:34:47 2017
main 363...sat nov 11 04:35:08 2017
---------------
耗時為19s,反而耗時高了
系統:2核,2cpu
資料量:2000條
2核2cpu的,單程序:
2核2cpu,4個程序:main 361 ...sat nov 11 04:40:15 2017
main 363...sat nov 11 04:40:33 2017
---------------
耗時18s
測試環境三:main 361 ...sat nov 11 04:41:46 2017
main 363...sat nov 11 04:41:59 2017
---------------
耗時為13s
系統:2核,2cpu
資料量:4000條
2核2cpu的,單程序:
2核2cpu,4個程序:main 361 ...sat nov 11 05:30:22 2017
main 363...sat nov 11 05:30:52 2017
---------------
耗時30s
五,總結分析idmain 361 ...sat nov 11 05:29:08 2017
main 363...sat nov 11 05:29:28 2017
---------------
耗時20s
核心資料量
程序耗時1單核
2000
單程序18s2單核
2000
4程序19s34核
2000
單程序18s44核
2000
4程序13s54核
4000
單程序30s64核
4000
4程序20s
1和2對比
結論:
1. 多程序並沒有提高程式的效能,反而造成了一定的效能下降。
2. 有些情況多程序並不能提高程式的效能
產生原因:
1. 由於系統為單核系統,所以每次程式執行的時候只有乙個程序在使用cpu。
2. 由於該程式並沒有長時間io阻塞的情況,只存在複雜的計算。所以時間都耗費在cpu進行複雜計算上。
3. 由於多程序存在程序間的切換,會消耗一定的時間,所以導致耗時反而比單程序時間還長。
1和3對比
結論 1. 多核對於單程序來說沒有任何效能上的提公升
產生的原因:
1. 多核單程序,實際上每次只有乙個程序在使用cpu。
2和4對比
結論:
1. 多核可以明顯提公升程式的效能。
產生原因:
1. 每個cpu可以並行的執行程式,不像單核cpu那樣是交替併發的執行。
5和6對比
結論 1. 資料量越大多核效能優勢體現的越明顯。
產生的原因:
1. 理想情況應該是多乙個核,效能提公升翻一倍,但是由於存在一些程序間的切換,所以沒法達到理想情況。資料量越大消耗的時間越多,效能翻倍之後,消耗時間也會翻倍的減少,所以數量越大效果越明顯。
六,附上程式原始碼
#coding=utf-8
from datetime import datetime
import threading
from elasticsearch import elasticsearch
from elasticsearch import helpers
from openpyxl import load_workbook
import sys
import time
from multiprocessing import process
reload(sys)
sys.setdefaultencoding('utf-8')
def(content):
es = elasticsearch(hosts=["xx.xx.xx.xx:9200"], timeout=5000)
actions =
i=1
index = 0
#print "start generate index..."
for line in content:
try:
action=
} except:
"*****=="
print line
"+++++++"
i+=1
if(len(actions)==500):
success, _ = helpers.bulk(es, actions, raise_on_error=true)
actions =
#del actions[0:len(actions)]
if (len(actions) > 0):
helpers.bulk(es, actions, raise_on_error=true)
defreadfile
(): wb = load_workbook(filename=r'~/myfile.xlsx')
sheets = wb.get_sheet_names() # 獲取所有**(worksheet)的名字
sheet0 = sheets[0] # 第乙個**的名稱
ws = wb.get_sheet_by_name(sheet0) # 獲取特定的 worksheet
# 獲取**所有行和列,兩者都是可迭代的
rows = ws.rows
columns = ws.columns
return rows
defsendtoes
(rows, start, end):
content =
for row in rows[start:end]:
line = [col.value for col in row]
deftask
(): rows = readfile()
process_list =
count = 1000
for i in range(0,4):
return process_list
defmultirun
(): process_list = task()
for process in process_list:
process.start()
# 主程序中等待所有子程序退出
for process in process_list:
process.join()
defrun
(): rows = readfile()
count = 1000
for i in range(0,4):
sendtoes(rows,i*count,(i+1)*count - 1)
if __name__=='__main__':
"main 361 ..." + time.ctime()
#單程序執行
run()
#多程序執行
multirun()
"main 363..." + time.ctime()
python多程序 python多程序
當有多個非相關任務需要處理時,並行能大大提高處理速度。這裡簡要介紹python的multiprocessing模組。簡單多程序編寫 當我們任務數量確定而且比較少的時候,可以手動為每個任務指定乙個程序來執行。import multiprocessing as mp def f a print a if...
python多程序 Python多程序實踐
建立程序方式如下 可以通過lock鎖機制實現共享鎖,但比較常用的方式還是以上這些方式,效率更高,更安全。使用方式 構造 類方法 使用方式 構造 更多型別支援一般使用manager,支援的型別包括list,dict,namespace,lock,rlock,semaphore,boundedsemap...
python多程序 Python多程序程式設計詳解
本文 在 python 3.6 環境下測試通過。多程序 multiprocessing 模組是在 python 2.6 版本中加入的,和多執行緒 threading 模組類似,都是用來做並行運算的。不過python既然有了threading,為什麼還要搞乙個multiprocessing呢?這是因為...