Reputation: 29465
The sum of sum1
and sum2
must be equal to 499500
in the end of this code, but it prints 0
, why?
import multiprocessing
sum1 = 0
sum2 = 0
def list_append_1(out_list):
global sum1
for i in out_list:
sum1 += i
print "sum1: ", sum1
def list_append_2(out_list):
global sum2
for i in out_list:
sum2 += i
print "sum2: ", sum2
if __name__ == "__main__":
lista_1 = [i for i in xrange(500)]# Number of random numbers to add
lista_2 = [i for i in xrange(500,1000)]
procs = 2 # Number of processes to create
# Create a list of jobs and then iterate through
# the number of processes appending each process to
# the job list
jobs = []
process_1 = multiprocessing.Process(target=list_append_1, args=(lista_1,))
process_2 = multiprocessing.Process(target=list_append_2, args=(lista_2,))
jobs.append(process_1)
jobs.append(process_2)
# Start the processes (i.e. calculate the random number lists)
for j in jobs:
j.start()
# Ensure all of the processes have finished
for j in jobs:
j.join()
print sum1 + sum2
Upvotes: 0
Views: 349
Reputation: 94951
This is happening because each of the tasks that you run using multiprocessing.Process
end up with their own copy of the sum1
and sum2
variables, along with a third copy held by the parent process. This is one of the limitations of using processes instead of threads; memory isn't shared between the processes by default. You can get the sums back by using a multiprocessing.Queue
to send the sums to the parent process:
import multiprocessing
def list_append(out_list, q):
my_sum = sum(out_list)
print "sum: ", my_sum
q.put(my_sum)
if __name__ == "__main__":
lista_1 = [i for i in xrange(500)]# Number of random numbers to add
lista_2 = [i for i in xrange(500,1000)]
procs = 2 # Number of processes to create
# Create a list of jobs and then iterate through
# the number of processes appending each process to
# the job list
jobs = []
q = multiprocessing.Queue()
process_1 = multiprocessing.Process(target=list_append, args=(lista_1, q))
process_2 = multiprocessing.Process(target=list_append, args=(lista_2, q))
jobs.append(process_1)
jobs.append(process_2)
# Start the processes (i.e. calculate the random number lists)
for j in jobs:
j.start()
total = q.get() + q.get()
# Ensure all of the processes have finished
for j in jobs:
j.join()
print total
Output:
sum1: 124750
sum2: 374750
499500
Upvotes: 6