How to print results of Python ThreadPoolExecutor.map immediately?

AKG picture AKG · Oct 27, 2014 · Viewed 7.4k times · Source

I am running a function for several sets of iterables, returning a list of all results as soon as all processes are finished.

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

How can I print intermediary results e.g. for variable1 = 1, variable2 = 7 as soon as their results are calculated?

Answer

Colonel Thirty Two picture Colonel Thirty Two · Oct 27, 2014

map already does this, but join needs to consume the entire iterable in order to create the joined string. Changing this to a for loop will let you print it incrementally:

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

Keeping the same output as the join code is a bit more work, but doable regardless:

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="")