![](/img/trans.png)
[英]No performance gain after using multiprocessing for a queue-oriented function
[英]Using multiprocessing to process image but no performance gain with 8 cpus
我得到了一個名為cartoonize(image_path)
的函數,該函數將'path / to / image'作為參數。 該腳本需要花費幾分鍾才能處理1920x1080尺寸的圖像。 我嘗試通過multiprocessing module
使用所有8 cores
,但是以下代碼沒有提高性能。 另一個問題是如何保存圖像。 它返回一個CV2對象。 通常,我通過see below code
將圖像保存到磁盤,但是通過多處理,它會給出錯誤"img is not a numpy array, neither a scalar."
我還希望獲得性能提升,但我不知道如何有效地做到這一點。
out_final = cartoonize('path/to/image'))
cv2.imwrite('cartoon.png', out_final)
import multiprocessing
if __name__ == '__main__':
# mark the start time
startTime = time.time()
print "cartoonizing please wait ..."
pool = multiprocessing.Pool(processes=multiprocessing.cpu_count())
pool_outputs = pool.apply_async(cartoonize, args=(image_path,))
pool.close()
pool.join()
print ('Pool:', pool_outputs)
# mark the end time
endTime = time.time()
print('Took %.3f seconds' % (startTime - endTime))
問題 :多重處理給出錯誤“ img不是一個numpy數組,也不是標量。”
pool_outputs = pool.apply_async(cartoonize, args=(image_path,))
您的代碼返回AsyncResult
Python»3.6.1文檔 » multiprocessing.pool.AsyncResult
class multiprocessing.pool.AsyncResult Pool.apply_async()和Pool.map_async() 返回的結果的類。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.