我有一个数组(称为 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模板网!
Python 多处理模块的 .join() 方法到底在做什么?What exactly is Python multiprocessing Module#39;s .join() Method Doing?(Python 多处理模块的 .join() 方法到底在做什么?)
在 Python 中将多个参数传递给 pool.map() 函数Passing multiple parameters to pool.map() function in Python(在 Python 中将多个参数传递给 pool.map() 函数)
multiprocessing.pool.MaybeEncodingError: 'TypeError("multiprocessing.pool.MaybeEncodingError: #39;TypeError(quot;cannot serialize #39;_io.BufferedReader#39; objectquot;,)#39;(multiprocessing.pool.MaybeEnc
Python 多进程池.当其中一个工作进程确定不再需要Python Multiprocess Pool. How to exit the script when one of the worker process determines no more work needs to be done?(Python 多进程池.当其中一
如何将队列引用传递给 pool.map_async() 管理的函数How do you pass a Queue reference to a function managed by pool.map_async()?(如何将队列引用传递给 pool.map_async() 管理的函数?)
与多处理错误的另一个混淆,“模块"对象没yet another confusion with multiprocessing error, #39;module#39; object has no attribute #39;f#39;(与多处理错误的另一个混淆,“模块对象