Python programming! Please help! Please include threads One master thread aggregating and sum the result of n slave-threads that each process sums a different range of values in a given array of 1000 random integers. Each thread will receive an segment of array of numbers, it will fork/spawn two new slaves each perform summing on different half of the received array segment, and the parent aggregate and return the total from its two slaves to its parent thread. Start with 7 nodes thread tree, when you are comfortable, you can extend it to a full thread tree. Given code: import multiprocessing import array as arr def find_sum(a,n1,n2,q): s=0 for i in range(n1-1,n2): s=s+a[i] q.put(s)
Python programming! Please help! Please include threads
One master thread aggregating and sum the result of n slave-threads that each process sums a different range of values in a given array of 1000 random integers. Each thread will receive an segment of array of numbers, it will fork/spawn two new slaves each perform summing on different half of the received array segment, and the parent aggregate and return the total from its two slaves to its parent thread. Start with 7 nodes thread tree, when you are comfortable, you can extend it to a full thread tree.
Given code:
import multiprocessing
import array as arr
def find_sum(a,n1,n2,q):
s=0
for i in range(n1-1,n2):
s=s+a[i]
q.put(s)
if __name__ == "__main__":
a=arr.array('i',[0,1,2,3,4,5,6,7,8,9])
q = multiprocessing.Queue()
# creating processes
p1 = multiprocessing.Process(target=find_sum, args=(a,1,4,q ))
p2 = multiprocessing.Process(target=find_sum, args=(a,5,7,q))
p3 = multiprocessing.Process(target=find_sum, args=(a,8,10,q))
sumarray=0
# starting process 1
p1.start()
p1.join()
# starting process 2
p2.start()
p2.join()
# starting process 3
p3.start()
p3.join()
for i in range(0,3):
sumarray+=q.get()
print("Array sum: ",sumarray)
Trending now
This is a popular solution!
Step by step
Solved in 2 steps