简体   繁体   English

从多进程函数调用中检索和合并返回的数组的有效方法

[英]Efficient way to retrieve and merge returned arrays from multiprocess function call

def get_version_list(env):
  list = []
  #do some intensive work
  return list

if __name__ == '__main__':
  from multiprocessing import Pool

  pool = Pool()

  result1 = pool.apply_async(get_version_list, ['prod'])
  result2 = pool.apply_async(get_version_list, ['uat'])
  #etc, I have six environment to check.

  alist = result1.get()
  blist = result2.get()

Ideally, I would like to have a list containing my six environments, and loop on that list to call my function for each environment (in parallel) then unite all the returned lists. 理想情况下,我希望有一个包含六个环境的列表,并在该列表上循环以针对每个环境(并行)调用我的函数,然后合并所有返回的列表。 The united list should not contain a value multiple times. 联合列表不应多次包含值。

something like so (I knwo that code does not work, but it's to give an idea of what wish to do) 像这样(我知道该代码不起作用,但这是为了让您知道要做什么)

if __name__ == '__main__':
  from multiprocessing import Pool

  pool = Pool()

  env = ['uat', 'prod', 'lt', 'lt2', 'cert', 'sin']

  for e in env:
      result = pool.apply_async(get_version_list, [e])

  #Merging the lists of the 6 function calls (unique entries)
  list = result.get()

Is there a simple way to do it? 有简单的方法吗?

Use map_async instead of apply_async . 使用map_async而不是apply_async

pool = Pool()
env = ['uat', 'prod', 'lt', 'lt2', 'cert', 'sin']
x = pool.map_async(get_version_list, env)

And now x will be a list of the results. 现在x将是结果列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM