I want to accumulate a sum using multiprocessing.Pool. Here's how I tried:
import multiprocessing
def add_to_value(addend, value):
value.value += addend
with multiprocessing.Manager() as manager:
value = manager.Value(float, 0.0)
with multiprocessing.Pool(2) as pool:
pool.starmap(add_to_value,
[(float(i), value) for i in range(100)])
print(value.value)
This gives incorrect and even inconsistent results. For instance, one time it gives 2982.0 and another it gives 2927.0. The correct output is 4950.0, and I do get this when I use only one process in the call to Pool, rather than 2. I'm using Python 3.7.5.