[英]Multiprocessing of summing 2 lists doesn't work in python
The sum of sum1
and sum2
must be equal to 499500
in the end of this code, but it prints 0
, why? 此代码末尾的
sum1
和sum2
之和必须等于499500
,但是它显示0
,为什么呢?
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
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. 发生这种情况的原因是,使用
multiprocessing.Process
运行的每个任务最终都有自己的sum1
和sum2
变量副本以及父进程拥有的第三个副本。 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: 您可以通过使用
multiprocessing.Queue
将总和发送到父进程来取回总和:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.