1)I am trying to do 8 parallel processes using concurrentFutures. The code is working as expected. But the results are printed only after all the processes are completed. I want the results printed as soon as they are available (not after when all the processes are done). How can I do that?
2)The "do_it" function is not printing anything in IDLE environment, only print commands in the main() function are printed on the IDLE window. But print commands in "do_it" are printing on online python compiler window (https://www.programiz.com/python-programming/online-compiler/). why?
I am using python3.9.5 on Windows OS.
import concurrent.futures
import time
def do_it(x):
print(f'sleeping for {x}')
start = time.perf_counter()
time.sleep(x)
end = time.perf_counter()
tTaken = round(end - start, 2)
return ['done sleeping', tTaken]
def main():
start = time.perf_counter()
with concurrent.futures.ProcessPoolExecutor() as executor:
delays = [1,2,3,4,5,6,7,8]
results = [executor.submit(do_it, x) for x in delays]
for f in concurrent.futures.as_completed(results):
[txt, duration] = f.result()
print(f'{txt} : time taken {duration}')
end = time.perf_counter()
tTaken = round(end - start, 2)
print(f'total time taken : {tTaken}')
if __name__ == '__main__':
main()