繁体   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()

理想情况下,我希望有一个包含六个环境的列表,并在该列表上循环以针对每个环境(并行)调用我的函数,然后合并所有返回的列表。 联合列表不应多次包含值。

像这样(我知道该代码不起作用,但这是为了让您知道要做什么)

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()

有简单的方法吗?

使用map_async而不是apply_async

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

现在x将是结果列表。

暂无
暂无

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

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