python: concurrent.futures.as_completed所有流程完成后才打印结果
python: concurrent.futures.as_completed printing results only after all the processes are completed
1)我正在尝试使用 concurrentFutures 执行 8 个并行进程。代码按预期工作。但是只有在所有过程完成后才会打印结果。我希望在结果可用时立即打印结果(而不是在所有过程完成之后)。我该怎么做?
2)“do_it”函数在 IDLE 环境中不打印任何内容,只有 main() 函数中的打印命令在 IDLE window 上打印。但是“do_it”中的打印命令正在在线 python 编译器 window (https://www.programiz.com/python-programming/online-compiler/) 上打印。为什么?
我在 Windows OS 上使用 python3.9.5。
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()
您只能在as_completed
执行程序完成后开始。
尝试 运行 而执行程序仍然存在:
def main():
start = time.perf_counter()
print("Submitting jobs")
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]
print("Running as_completed", flush=True)
for f in concurrent.futures.as_completed(results):
[txt, duration] = f.result()
print(f"{txt} : time taken {duration}", flush=True)
end = time.perf_counter()
tTaken = round(end - start, 2)
print(f"total time taken : {tTaken}")
结果应该是这样的:
Submitting jobs
sleeping for 1
sleeping for 2
sleeping for 3
Running as_completed
sleeping for 4
sleeping for 5
done sleeping : time taken 1.04
sleeping for 6
done sleeping : time taken 2.03
sleeping for 7
done sleeping : time taken 3.0
sleeping for 8
done sleeping : time taken 4.01
done sleeping : time taken 5.05
done sleeping : time taken 6.07
done sleeping : time taken 7.01
done sleeping : time taken 8.03
total time taken : 12.08
编辑
关于子进程丢失的输出,这与 Python 如何在 ms-windows 上启动新进程有关。与 UNIX/POSIX 系统相反,文件句柄 而不是 由子进程继承。因此,您不会在 ms-windows 上看到子进程的 print
输出。猜测,在线python在UNIX/POSIX系统上是运行。
1)我正在尝试使用 concurrentFutures 执行 8 个并行进程。代码按预期工作。但是只有在所有过程完成后才会打印结果。我希望在结果可用时立即打印结果(而不是在所有过程完成之后)。我该怎么做?
2)“do_it”函数在 IDLE 环境中不打印任何内容,只有 main() 函数中的打印命令在 IDLE window 上打印。但是“do_it”中的打印命令正在在线 python 编译器 window (https://www.programiz.com/python-programming/online-compiler/) 上打印。为什么?
我在 Windows OS 上使用 python3.9.5。
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()
您只能在as_completed
执行程序完成后开始。
尝试 运行 而执行程序仍然存在:
def main():
start = time.perf_counter()
print("Submitting jobs")
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]
print("Running as_completed", flush=True)
for f in concurrent.futures.as_completed(results):
[txt, duration] = f.result()
print(f"{txt} : time taken {duration}", flush=True)
end = time.perf_counter()
tTaken = round(end - start, 2)
print(f"total time taken : {tTaken}")
结果应该是这样的:
Submitting jobs
sleeping for 1
sleeping for 2
sleeping for 3
Running as_completed
sleeping for 4
sleeping for 5
done sleeping : time taken 1.04
sleeping for 6
done sleeping : time taken 2.03
sleeping for 7
done sleeping : time taken 3.0
sleeping for 8
done sleeping : time taken 4.01
done sleeping : time taken 5.05
done sleeping : time taken 6.07
done sleeping : time taken 7.01
done sleeping : time taken 8.03
total time taken : 12.08
编辑
关于子进程丢失的输出,这与 Python 如何在 ms-windows 上启动新进程有关。与 UNIX/POSIX 系统相反,文件句柄 而不是 由子进程继承。因此,您不会在 ms-windows 上看到子进程的 print
输出。猜测,在线python在UNIX/POSIX系统上是运行。