3
我需要加載約100k文件與載體和聚合內容在一個numpy數組。這個過程大約需要3分鐘,所以我想加快速度。我試圖用gevent來加速它,但我無法獲得任何加速。沒有加速讀取文件與gevent
我讀到應該使用異步調用來加速IO調用而不是多處理。我進一步閱讀gevent是推薦的圖書館。我寫了一個例子來下載圖片,我可以看到速度的巨大提高。這裏是我的代碼
def chunks(l, n):
""" Yield successive n-sized chunks from l.
"""
for i in xrange(0, len(l), n):
yield l[i:i+n]
file_paths = # list of filenames
numpy_array = numpy.ones([len(file_paths), file_size])
pool = gevent.pool.Pool(poolsize)
for i, list_file_path_tuples in enumerate(chunks(file_paths, CHUNK_SIZE)):
gevent_results = pool.map(numpy.load, list_file_path_tuples)
pool.join()
for i_chunk, result in enumerate(gevent_results):
index = i * CHUNK_SIZE + i_chunk
data = result['arr_0']
numpy_array[index] = data
使用塊是必要的簡化版本,否則我會所有的載體兩次在內存中。
在我的代碼中存在問題還是使用錯誤的方法?