我有以下示例:
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
我希望这个函数会按照每次睡眠的顺序打印“我睡了 {} 秒”,但是,在处理完最后一个值 (10) 之后会打印所有结果。
为什么会发生这种情况以及每次通话完成时我将如何打印 sleeper