問題描述
我有一個(gè)數(shù)組(稱為 data_inputs
),其中包含數(shù)百個(gè)天文圖像文件的名稱.然后對(duì)這些圖像進(jìn)行處理.我的代碼有效,并且需要幾秒鐘來處理每個(gè)圖像.但是,它一次只能做一個(gè)圖像,因?yàn)槲艺谕ㄟ^ for
循環(huán)運(yùn)行數(shù)組:
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
沒有理由我必須先修改圖像,所以是否可以利用我機(jī)器上的所有 4 個(gè)核心,每個(gè)核心在不同的圖像上通過 for 循環(huán)運(yùn)行?
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?
我已閱讀有關(guān) multiprocessing
模塊的信息,但我不確定如何在我的情況下實(shí)現(xiàn)它.我熱衷于讓 multiprocessing
工作,因?yàn)樽罱K我必須在 10,000 多張圖像上運(yùn)行它.
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.
推薦答案
你可以簡(jiǎn)單地使用 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
這篇關(guān)于多處理一個(gè)for循環(huán)?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!