gpt4 book ai didi

函数内的Python多处理: calling pool.映射

转载 作者:可可西里 更新时间:2023-11-01 09:55:12 26 4
gpt4 key购买 nike

我正在尝试使用 mutltiprocessing 包在一个函数中使用多个 CPU。当我在函数外部运行玩具示例时,它会在四分之一秒内运行,没有任何问题(见下文)。

from multiprocessing import Pool
import time

start = time.clock()

def f(x):
return x*x

if __name__ == '__main__':
with Pool(processes=7) as pool:
result = pool.map(f, range(1000))



print(time.clock() - start)

但是,当我将相同的代码改编成一个函数时(见下文),它会打印 True 以指示 __name__ == '__main__',但随后它会运行永远永远不会返回结果。我在 Windows 7 上运行 Python 3.3。

from multiprocessing import Pool
import time

start = time.clock()

def f(x):
return x*x

def testfunc(r):
if __name__ == '__main__':
print(True)
with Pool(processes=7) as pool:
result = pool.map(f, range(r))

return result

result = testfunc(1000)
print(time.clock() - start)

最佳答案

您在错误的地方使用了 if __name__ == '__main__'

from multiprocessing import Pool
import time

start = time.clock()

def f(x):
return x*x

def testfunc(r):
print(True)
with Pool(processes=7) as pool:
result = pool.map(f, range(r))
return result

if __name__ == '__main__':
result = testfunc(1000)
print(time.clock() - start)

根据 multiprocessing - Programming guidelines :

Safe importing of main module

Make sure that the main module can be safely imported by a new Python interpreter without causing unintended side effects (such a starting a new process).

... one should protect the “entry point” of the program by using if __name__ == '__main__': as follows:

关于函数内的Python多处理: calling pool.映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19011540/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com