問題描述
我有一個數組(稱為 data_inputs
),其中包含數百個天文圖像文件的名稱.然后對這些圖像進行處理.我的代碼有效,并且需要幾秒鐘來處理每個圖像.但是,它一次只能做一個圖像,因為我正在通過 for
循環運行數組:
I have an array (called data_inputs
) containing the names of hundreds of astronomy images files. These images are then manipulated. My code works and takes a few seconds to process each image. However, it can only do one image at a time because I'm running the array through a for
loop:
for name in data_inputs:
sci=fits.open(name+'.fits')
#image is manipulated
沒有理由我必須先修改圖像,所以是否可以利用我機器上的所有 4 個核心,每個核心在不同的圖像上通過 for 循環運行?
There is no reason why I have to modify an image before any other, so is it possible to utilise all 4 cores on my machine with each core running through the for loop on a different image?
我已閱讀有關 multiprocessing
模塊的信息,但我不確定如何在我的情況下實現它.我熱衷于讓 multiprocessing
工作,因為最終我必須在 10,000 多張圖像上運行它.
I've read about the multiprocessing
module but I'm unsure how to implement it in my case.
I'm keen to get multiprocessing
to work because eventually I'll have to run this on 10,000+ images.
推薦答案
你可以簡單地使用 multiprocessing.Pool
:
You can simply use multiprocessing.Pool
:
from multiprocessing import Pool
def process_image(name):
sci=fits.open('{}.fits'.format(name))
<process>
if __name__ == '__main__':
pool = Pool() # Create a multiprocessing Pool
pool.map(process_image, data_inputs) # process data_inputs iterable with pool
這篇關于多處理一個for循環?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!