使用当前的代码,您实际上并没有
CURRENT_SUCCESSES在进程之间共享。
callback在主进程的结果处理线程中执行。只有一个结果处理线程,因此每个线程只能一次
callback运行,而不是同时运行。因此,您编写的代码是进程/线程安全的。
但是,您忘记了要修复的
successes从返回
func。
编辑:
另外,使用以下代码可以更简洁地编写
map:
def func(inputs): successes = [] for input in inputs: result = #something with return pre if result == 0: successes.append(input) return successesdef main(): pool = mp.Pool() total_successes = pool.map(func, myInputs) # Returns a list of lists # Flatten the list of lists total_successes = [ent for sublist in total_successes for ent in sublist]



