sgalpha01
sgalpha01

Reputation: 373

Python's ProcessPoolExecutor is giving output of print and return statement in reverse order

I was practicing using multiprocessing in Python, so I came across concurrent.futures module. I tried running the following code:

import concurrent.futures
import time


def do_something(seconds):
    print(f'Sleeping {seconds} second(s)...')
    time.sleep(seconds)
    return f'Done Sleeping for {seconds} second(s)'

def main():

    with concurrent.futures.ProcessPoolExecutor() as executor:
        results = [executor.submit(do_something, 1.5) for _ in range(2)]

        for f in concurrent.futures.as_completed(results):
            print(f.result())


if __name__ == '__main__':
    start = time.perf_counter()
    main()
    finish = time.perf_counter()
    print(f'Finished in {round(finish-start, 2)} second(s)')

I should expect something like the following as output:

Sleeping 1.5 second(s)...
Sleeping 1.5 second(s)...
Done Sleeping for 1.5 second(s)
Done Sleeping for 1.5 second(s)
Finished in 1.5 second(s)

But, I got:

Done Sleeping for 1.5 second(s)
Done Sleeping for 1.5 second(s)
Sleeping 1.5 second(s)...
Sleeping 1.5 second(s)...
Finished in 1.83 second(s)

Isn't it weird that the return statement is coming before the print statement? Here is the exact snip: sublime_snip

Upvotes: 5

Views: 3483

Answers (1)

Tyler
Tyler

Reputation: 1393

This may have to do with standard out buffering. Try passing flush=True in your print statement like:

print(f'Sleeping {seconds} second(s)...', flush=True)

You can see this is a pretty common issue with multiprocessed printing: Python multithreaded print statements delayed until all threads complete execution

Upvotes: 9

Related Questions