python 使用 joblib 进行简单的并行计算
python simple parallel computation with 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
而且它运行的时间太长了。我错过了什么?
错误消息和 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
巴尼
我不得不将 backend="threading" 添加到 barny 的代码到 运行 而没有错误:
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)
我以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
而且它运行的时间太长了。我错过了什么?
错误消息和 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 巴尼
我不得不将 backend="threading" 添加到 barny 的代码到 运行 而没有错误:
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)