Question
How to do it using threads? # importing the multiprocessing module 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]
How to do it using threads?
# importing the multiprocessing module 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)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started