1

I have the following example:

import time
import concurrent.futures
x = [10, 1, 2]

def sleeper(secs):
    time.sleep(secs)
    print('I slept for {} seconds'.format(secs))

# returns in the order given
with concurrent.futures.ThreadPoolExecutor(max_workers=3) as executor:
    for future in executor.map(sleeper, x):
        future

I would expect that this function will print "I slept for {} seconds" in order that of each sleeping, however, ALL the results are printed after the last value (10) is processed.

Why is this occurring and how would I print sleeper as each call completes

shx2
  • 61,779
  • 13
  • 130
  • 153
user2726995
  • 2,064
  • 2
  • 21
  • 26
  • 3
    Does [`sys.stdout.flush()`](http://stackoverflow.com/q/230751/190597) fix the problem? (By the way, when I run your code, the print statements are printed one at a time. So I'm thinking the problem might not be in the Python code but in how the terminal or IDE is buffering the output.) – unutbu Mar 25 '15 at 19:12
  • I am in an ipython notebook so that might be the case – user2726995 Mar 25 '15 at 19:17

1 Answers1

0

I tested with Python2 and 3 in terminal, it works as expected. With IPython Notebook, all the results are printed after the last value (10) is processed.

So it is a problem of IPython Notebook, maybe you should submit a issue.

laike9m
  • 18,344
  • 20
  • 107
  • 140