0

我以joblib 教程为例。这是我的代码的样子:

from math import sqrt
from joblib import Parallel, delayed
import multiprocessing

test = Parallel(n_jobs=2)(delayed(sqrt)(i ** 2) for i in range(10))
print(test)

它产生以下错误消息:

Attempting to do parallel computing without protecting your import
on a system that does not support forking. To use parallel-computing 
in a script, you must protect you main loop using 
"if __name__ == '__main__'". Please see the joblib documentation on 
Parallel for more information

而且运行时间太长。我错过了什么?

4

2 回答 2

4

错误消息和 BrenBran 告诉您的是 a)您应该阅读错误消息,并且 b)您应该组织您的代码,例如:

from math import sqrt
from joblib import Parallel, delayed
import multiprocessing

if __name__ == '__main__':
    test = Parallel(n_jobs=2)(delayed(sqrt)(i ** 2) for i in range(10))
    print(test)

HTH 谷仓

于 2015-08-31T19:45:24.323 回答
0

我必须在 barny 的代码中添加 backend="threading" 才能正常运行:

from math import sqrt
from joblib import Parallel, delayed
import multiprocessing

if __name__ == '__main__':
    test = Parallel(n_jobs=2, backend="threading")(delayed(sqrt)(i ** 2) for i in range(10))
print(test)
于 2018-03-06T18:47:28.400 回答