[英]Multiprocessing & Pool in __main__ - how to get the output outside the __main__?
Based on this answer ( https://stackoverflow.com/a/20192251/9024698 ), I have to do this:基于这个答案( https://stackoverflow.com/a/20192251/9024698 ),我必须这样做:
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
to multi-process a for
loop.多处理一个
for
循环。
However, I am wondering, how can I get the output of this and further process if I want?但是,我想知道,如果我愿意,我怎样才能得到这个和进一步处理的 output?
It must be like that:应该是这样的:
if __name__ == '__main__':
pool = Pool() # Create a multiprocessing Pool
output = pool.map(process_image, data_inputs) # process data_inputs iterable with pool
# further processing
But then this means that I have to put all the rest of my code in __main__
unless I write everything in functions which are called by __main__
?但这意味着我必须将我的代码的所有 rest 放在
__main__
中,除非我将所有内容都写在由__main__
调用的函数中?
The notion of __main__
has been always pretty confusing to me. __main__
的概念一直让我很困惑。
if __name__ == '__main__':
is literally just "if this file is being run as a script, as opposed to being imported as a module, then do this". if __name__ == '__main__':
字面上只是“如果此文件作为脚本运行,而不是作为模块导入,则执行此操作”。 __name__
is a hidden variable that gets set to '__main__'
if it's being run as a script. __name__
是一个隐藏变量,如果它作为脚本运行,它会被设置为'__main__'
。 why it works this way is beyond the scope of this discussion but suffice it to say it has to do with how python evaluates sourcefiles top-to-bottom.为什么它以这种方式工作超出了本次讨论的 scope 但足以说明它与 python 如何从上到下评估源文件有关。
In other words, you can put the other two lines anywhere you want - in a function, probably, that you call elsewhere in the program.换句话说,您可以将其他两行放在您想要的任何位置 - 在 function 中,可能是您在程序的其他地方调用的。 You could return
output
from that function, or do other processing on it, or etc., whatever you happen to need.您可以从该 function 返回
output
,或者对其进行其他处理,等等,无论您碰巧需要什么。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.