Wednesday 15 June 2011

How to print results of Python ThreadPoolExecutor.map immediately? -



How to print results of Python ThreadPoolExecutor.map immediately? -

i running function several sets of iterables, returning list of results processes finished.

def fct(variable1, variable2): # operation not take same amount of # time different input variables , yields result1 , result2 homecoming result1, result2 variables1 = [1,2,3,4] variables2 = [7,8,9,0] threadpoolexecutor(max_workers = 8) executor: future = executor.map(fct,variables1,variables2) print '[%s]' % ', '.join(map(str, future)) >>> [ (12,3) , (13,4) , (14,5) , (15,6) ]

how can print intermediary results e.g. variable1 = 1, variable2 = 7 results calculated?

map this, join needs consume entire iterable in order create joined string. changing for loop allow print incrementally:

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

keeping same output join code bit more work, doable regardless:

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

python map threadpool concurrent-programming concurrent.futures

No comments:

Post a Comment