如何立即打印Python ThreadPoolExecutor.map的结果



我正在为多组可迭代项运行一个函数,在所有进程完成后立即返回所有结果的列表。

def fct(variable1, variable2):
   # do an operation that does not necessarily take the same amount of
   # time for different input variables and yields result1 and result2
   return result1, result2
variables1 = [1,2,3,4]
variables2 = [7,8,9,0]
with ThreadPoolExecutor(max_workers = 8) as executor:
    future = executor.map(fct,variables1,variables2)
    print '[%s]' % ', '.join(map(str, future))

>>> [ (12,3) , (13,4) , (14,5) , (15,6) ]

如何在计算出中间结果后立即打印中间结果,例如variable1=1、variable2=7?

如果您想在完成时使用结果,而不保留原始可迭代的顺序,则可以将executor.submitconcurrent.futures.as_completed:一起使用

from concurrent.futures import ThreadPoolExecutor, as_completed
import time
import random
def fct(variable1, variable2):
   time.sleep(random.randint(1,5))
   return variable1+1, variable2+1
variables1 = [1,2,3,4]
variables2 = [7,8,9,0]
with ThreadPoolExecutor(max_workers = 8) as executor:
    for out in as_completed([executor.submit(fct,*vars) 
                                for vars in zip(variables1, variables2)]):
        print(out.result())

输出(尽管由于random.randint,在任何给定的运行中都可能有任何订单):

(4, 10)
(5, 1)
(2, 8)
(3, 9)

一旦Future被标记为完成,as_completed将从其输入列表中产生Future,而不管它实际位于输入列表中的何处。这样,如果第二项在2秒后完成,但第一项需要15秒,那么您将在两秒后看到第二项的结果,而不需要等待15秒。根据您的具体用例,这可能是理想的行为,也可能不是理想的行为。

编辑:

请注意,您仍然可以通过这种方式以原始顺序获得输出。你只需要保存你给as_completed:的列表

with ThreadPoolExecutor(max_workers = 8) as executor:
    jobs = [executor.submit(fct, *vars) 
               for vars in zip(variables1, variables2)]
    for out in as_completed(jobs):
        print(out.result())
    results = [r.result() for r in jobs]
    print(results)

输出:

(5, 1)
(2, 8)
(3, 9)
(4, 10)
[(2, 8), (3, 9), (4, 10), (5, 1)]

map已经这样做了,但join需要使用整个可迭代项才能创建连接字符串。将其更改为for循环将允许您增量打印:

for i in executor.map(fct, v1, v2):
    print(str(i))

保持与join代码相同的输出需要更多的工作,但无论如何都是可行的:

first = True
print("[ ", end="")
for i in executor.map(fct, v1, v2):
    if first:
        first = False
    else:
        print(" , ", end="")
    print(str(i), end="")
print("]", end="")

相关内容

  • 没有找到相关文章

最新更新