简体   繁体   English

apply_async 结果顺序

[英]apply_async order of results

If I understand correctly, apply_async returns immediately with an AsyncResult object.如果我理解正确,apply_async 会立即返回一个 AsyncResult 对象。 If I collect those objects in the following way, and use get() only after all workers have finished, is it safe to assume the values will be in the order the function was called?如果我按以下方式收集这些对象,并仅在所有工作人员完成后才使用 get(),假设值将按照调用函数的顺序是否安全?

objRes = [None] * len(aRange)
pool = Pool(processes=8)
for x in aRange:
    objRes[x] = pool.apply_async(f,(arg1, arg2, arg3,))
pool.close()
pool.join()

res = [None] * len(aRange)
for x in aRange:
    res[x] = objRes[x].get()

I started out trying to use pool.map() and pool.starmap() instead, but I have two arrays as part of the arguments and couldn't figure out how exactly to pass them.我开始尝试使用 pool.map() 和 pool.starmap() 代替,但我有两个数组作为参数的一部分,无法弄清楚如何确切地传递它们。

The AsyncResult object returned by the apply_async function is associated to the tuple function-parameters you scheduled. apply_async函数返回的AsyncResult对象与您安排的元组函数参数相关联。

Hence, when you call AsyncResult.get you will get the results of that given job.因此,当您调用AsyncResult.get您将获得该给定作业的结果。

If you put the AsyncResult objects in a list with the same order as you scheduled the jobs then you will get the results in that order.如果您将AsyncResult对象放在一个列表中,其顺序与您安排作业的顺序相同,那么您将按该顺序获得结果。

The map function internally works in a very similar fashion as in your example. map函数在内部的工作方式与您的示例非常相似。 If you want to pass more than a parameter to the scheduled function you need to group it with the rest of the arguments.如果要将多个参数传递给预定函数,则需要将其与其余参数分组。 It might be quite memory consuming if you have a lot of arguments.如果你有很多参数,它可能会非常消耗内存。

arg_list = ((arg1, arg2, arg3) for _ in aRange)

pool.map(f, arg_list)

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

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