Python多线程池Windows冻结
在上述代码中,nU=6040,因此在总I循环中Python多线程池Windows冻结,python,python-3.x,threadpool,python-multithreading,Python,Python 3.x,Threadpool,Python Multithreading,在上述代码中,nU=6040,因此在总I循环中36.481.600(6040*6040) 我的值位于一个大小为(60403060)的2d数组std_矩阵 对于std_矩阵中的每对行I计算余弦相似性: import multiprocessing as mp with mp.Pool(3) as pool: res = pool.starmap(sim.cosine, ((std_matrix[i], std_matrix[j]) for i in range(n
36.481.600(6040*6040)
我的值位于一个大小为(60403060)的2d数组std_矩阵
对于std_矩阵中的每对行
I计算余弦相似性:
import multiprocessing as mp
with mp.Pool(3) as pool:
res = pool.starmap(sim.cosine, ((std_matrix[i], std_matrix[j]) for i in range(nU) for j in range(nU)))
pool.close()
pool.join()
cos_matrix_uu = np.array(res).reshape(nU, nU)
当我在没有多线程的情况下运行这段代码时,它会执行,但在使用多线程的情况下,我的电脑会冻结。即使我使用3个线程
我的gpu是GTX850M。我可能做错了什么?您的任务是cpu密集型任务,而不是IO密集型任务。由于python的GIL,python的多线程不能分配到多核。因此,目前只有一个线程可以运行。它不适合此任务类型。
只使用多进程。你说的“只使用多进程”是什么意思?我以前也使用过ThreadPoolExecutor,但同样的情况也发生过。我现在用的不是多进程吗?是的,你用的是多进程。我的意思是不包括多线程。仅使用多过程。让我重新表述我的问题:让';假设我有一个大的二维浮点数数组。(5k,5k)尺寸(=25密耳值)。我想用数组中的每个浮点数计算一个函数,并将其重新分配到相同的位置。如何对其进行多线程处理,使python不会';你不会冻僵吧?这可能吗?已经说过,它不适合这个问题。但它肯定能做到。
def cosine(a, b):
"""Calculates the Cosine Similarity.
:param a: array of ratings
:param b: array of ratings
:return: Cosine Similarity
"""
divisor = (np.sqrt(np.dot(a, a)) * np.sqrt(np.dot(b, b)))
if divisor == 0:
return 0
return np.divide(np.dot(a, b), divisor)