python mutlthreading +带有列表和词典的有限数量的进程

时间:2017-12-11 13:47:10

标签: python multithreading dictionary

我试图多线程化一个在迭代执行时花费大量时间的函数。

简化版本将是:

from multiprocessing import Process, Manager

def f(my_dict, key, list1, list2):
    count = [0] * len(list1)
    for i, val1 in enumerate(list1):
        count[i] = sum(belongs_to(val1, val2) for val2 in list2)
    my_dict[key] = (sum(count))

manager = Manager()
my_dict = manager.dict()
job = [Process(target=f, args=(my_dict, record, value, list2)) 
        for record, value in other_dict.items()]
_ = [p.start() for p in job]
_ = [p.join() for p in job]
my_dict = {key: value for key, value in my_dict.items()}

当我运行此代码时,我的内存溢出。有没有简单的方法可以同时限制线程数?

此外,由于Manager,我正在共享字典,在所有线程之间接收答案。有没有办法分享给函数f的列表,因为list2总是一样的?

0 个答案:

没有答案