【问题标题】:Nothing is printed while using concurrent.futures使用 concurrent.futures 时不打印任何内容
【发布时间】:2021-08-23 19:16:20
【问题描述】:

我想让一个进程并行运行,所以我使用 concurrent.futures 。问题是它没有执行函数hello()。

import time
import concurrent.futures
def hello(name):
    print(f'hello {name}')
    sleep(1)

if __name__ == "__main__":
    t1=time.perf_counter()
    names=["Jack","John","Lily","Stephen"]


    with concurrent.futures.ProcessPoolExecutor() as executor:
        executor.map(hello,names)

    t2=time.perf_counter()
    print(f'{t2-t1} seconds')

输出

0.5415315 seconds

【问题讨论】:

  • 我得到了一个完全不同的输出,对我来说,它会等待sleep 中给出的时间,然后才能显示 perf_counter 时间
  • @azro 打印名字了吗?

标签: python parallel-processing python-multiprocessing concurrent.futures


【解决方案1】:

经过concurrent.futures documentation后发现ProcessPoolExecutor在交互式解释器中不起作用。所以你需要创建一个文件并通过命令提示符/bash shell 运行它。

【讨论】:

    猜你喜欢
    • 2020-11-23
    • 2019-05-26
    • 2021-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多