我使用多线程进行计算:
def calc_score(score, i, j, a, b):
score[i, j] = func(a, b)
if __name__ == '__main__':
df = pd.read_excel('df.xlsx')
db = pd.read_csv('db2.csv', converters= {'create_date': pd.to_datetime})
m = len(df)
n = len(db)
pool = Pool(multiprocessing.cpu_count())
score = np.zeros([m, n])
for i in range(m):
a = df.loc[i, 'val']
if a == 'None' or a == '':
score[i, :] = 0
else:
for j in range(n):
b = db.loc[j, 'val']
pool.apply_async(calc_score, (score, i, j, a, b))#multi-threading
pool.close()
pool.join()
我收到错误消息:
Traceback (most recent call last):
File "C:\main.py", line 107, in <module>
File "C:\Program Files\Python36\lib\multiprocessing\pool.py", line 356, in apply_async
File "C:\Program Files\Python36\lib\multiprocessing\pool.py", line 620, in __init__
RuntimeError: can't allocate lock
原因是什么以及如何解决?
我可以使用互斥锁锁定该部分吗?
更新:
mutex = threading.Lock()
def calc_score(score, i, j, a, b):
with mutex:
score[i, j] = func(a, b)
答案 0 :(得分:0)
我认为问题在于试图直接共享一个numpy矩阵。
我看到了这些选项。
multprocessing.Array
,但是有了numpy,它必须使用一些额外的技巧。