【发布时间】: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