简体   繁体   中英

Getting results from multiprocessing.process for a loop

The goal of this program is to calculate the loop_random for the loop num . I want to calculate 4 random different num/4 with 4 processors in parallel and in the end summing to give the u_total . But the problem is though I see all the 4 processors in action, the end result is calculated for 4 times the same set of num/4 . But I want to calculate loop_random for 4 different random arrays. Here is the snippet of the part of the code I am working on. .

from numpy import *
import multiprocessing as mp
from multiprocessing import Process, Queue
num = 5000
def loop_random(num, out):
    n = 1
    total = zeros(((100.,100.,100.)), dtype='float')
    while n<= num:
        x, y , z = random.rand(100), random.rand(100), random.rand(100) #some random numbers
        result = x**2 + y**2 + z**2
        total = total + result
        n += 1
    out.put(total)

if __name__=='__main__':
    q1 = mp.Queue()
    q2 = mp.Queue()
    q3 = mp.Queue()
    q4 = mp.Queue()
    p1 = mp.Process(target = loop_random, args=(num/4, q1))
    p2 = mp.Process(target = loop_random, args=(num/4, q2))
    p3 = mp.Process(target = loop_random, args=(num/4, q3))
    p4 = mp.Process(target = loop_random, args=(num/4, q4))
    p1.start()
    p2.start()
    p3.start()
    p4.start()
    u_total_proc1 = q1.get()
    u_total_proc2 = q2.get()
    u_total_proc3 = q3.get()
    u_total_proc4 = q4.get()
    p1.join()
    p2.join()
    p3.join()
    p4.join()
    u_total = u_total_proc1 + u_total_proc2 + u_total_proc3 + u_total_proc4
    print u_total

Any suggestions are welcome. The problem is more about is this the right way of multiprocessing? Does each Queue calculate the same result and not stored or each processor calculates different results and stored? Thanks a lot for your help. Please do correct me if there is something wrong in what I said.

Sorry for posting this answer, as it is not an answer, however I cannot post comments yet.

Just wanted to note that using:

from pylab import *

Is considered bad practise. Don't blanket import a bunch of random stuff. Each module should have a longish list of the specific things it needs.

According to the Python Zen:

Explicit is better than implicit.

Can't argue with that :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM