我想让一个进程并行运行,所以我使用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发布于 2021-06-07 12:56:43
浏览完concurrent.futures documentation后,我发现ProcessPoolExecutor在交互式解释器中不起作用。因此,您需要创建一个文件,并通过命令提示符/bash shell运行它。
https://stackoverflow.com/questions/67859171
复制相似问题