使用joblib的python简单并行计算

时间:2015-08-31 19:15:25

标签: python joblib

我从joblib tutorial举了示例。以下是我的代码的样子:

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

它运行的时间太长了。我错过了什么?

2 个答案:

答案 0 :(得分:4)

错误信息和BrenBran告诉你的是a)你应该阅读错误信息,b)你应该组织你的代码:

tokenTotals[i].substr(0, 28)

HTH 巴尼

答案 1 :(得分: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)