在对象列表上实现多线程

Implementing multithreading on a list of objects

def return_total():
    dE_total = 0
    for num in range(len(self.layer)):
        dE_total += self.layer[num].backprop(delta[num])
    return dE_total

我在class里面有上面的方法。我需要使用多线程调用 backprop() 方法。通常 self.layer 的长度很小。我正打算尝试 ThreadPoolExecutormap() 方法。据我所知,它用于调用函数和可迭代值。这里每个线程都应该使用输入参数执行不同的方法。有什么办法可以做到这一点吗?

with ThreadPoolExecutor() as executor:
    dE_total += executor.map(self.layer.backprop, delt)

我知道上面的代码没有任何意义。我正在寻找与上述想法类似的东西。

提前感谢您的帮助

如果我对此的解释正确,您可以编写一个将函数作为参数的方法。然后可以将其传递给 executor.map,例如:

def func_caller(funcs, params):
    return func(*params)

dE_total += sum(executor.map(func_caller, funcs_params))

或类似的,带有 funcs_params 一些适当的函数和参数元组列表。解包参数可能需要调整。