与此问题类似How to share a variable in 'joblib' Python library
我想在joblib中共享一个变量。但是,我的问题完全不同,我有一个巨大的变量(2-3Gb的RAM),我希望我的所有线程都从它读取。他们永远不会写,如:
def func(varThatChange, varToRead):
# Do something over varToRead depending on varThatChange
return results
def main():
results = Parallel(n_jobs=100)(delayed(func)(varThatChange, varToRead) for varThatChange in listVars)
我无法正常分享它,因为它需要大量时间来复制变量,而且,我会耗尽内存。
我该如何分享?
答案 0 :(得分:0)
您可以尝试multiprocessing.Manager
,例如:
import pandas as pd
import numpy as np
from joblib import Parallel, delayed
from multiprocessing import Manager
manager = Manager()
q = manager.Namespace()
q.arr = np.array([1,2,3,4])
def test(i, q):
return q.arr[i]
out = Parallel(n_jobs=4)(delayed(test)(i, q) for i in range(4))
退出:
[1, 2, 3, 4]