I hope I can explain this well enough. I'm suppose to make a main master thread that aggregates and sums together the work of several slave threads. The amount of slave threads is N and varies. These slave threads each individually produce an array 1000 random integers and then sums them together.
I have this code to start and it works but its all done using arrays:
from random import randint
from multiprocessing import *
from queue import Queue
q = Queue()
def array(n):
sumOfSlaves = 0
randomList = [0 for i in range(100)]
sizeOfArray = 1000 // n
if 1000 % n != 0:
for i in range((1000 % n)):
randomList = [(randint(0, 1000)) for i in range(int(1000 / n) + 1)]
print("Objects in array: ", randomList)
print("Size of array: ", len(randomList))
print("Sum of objects in array: ", sum(randomList))
print(sum(randomList))
sumOfSlaves = sumOfSlaves + sum(randomList)
print("Aggregate of slave threads so far: ", sumOfSlaves)
for i in range(n - int(1000 % n)):
randomList = [(randint(0, 1000)) for i in range(int(1000 / n))]
print("Objects in array: ", randomList)
print("Size of array: ", len(randomList))
print("Sum of objects in array: ", sum(randomList))
print(sum(randomList))
sumOfSlaves = sumOfSlaves + sum(randomList)
print("Aggregate of slave threads so far: ", sumOfSlaves)
return ()
else:
for i in range(n):
randomList = [(randint(0, 1000)) for i in range(sizeOfArray)]
print("Objects in array: ", randomList)
print("Size of array: ", len(randomList))
print("Sum of objects in array: ", sum(randomList))
sumOfSlaves = sumOfSlaves + sum(randomList)
print("Aggregate of slave threads so far: ", sumOfSlaves)
return ()
if __name__ == '__main__':
o = int(input("please enter how many slave processor you need: "))
array(o)
It works fine, but my goal is to do all this using threads instead. This is what I have done so far:
from random import randint
import logging
import threading
import time
def master_thread(n):
randomList = [0 for i in range(100)]
sizeOfArray = 1000 // n
sumOfSlaves = 0
if 1000 % n != 0:
for i in range((1000 % n)):
randomList = [(randint(0, 1000)) for i in range(int(1000 / n) + 1)]
print("Objects in array: ", randomList)
print("Size of array: ", len(randomList))
print("Sum of objects in array: ", sum(randomList))
print(sum(randomList))
sumOfSlaves = sumOfSlaves + sum(randomList)
for i in range(n - int(1000 % n)):
randomList = [(randint(0, 1000)) for i in range(int(1000 / n))]
print("Objects in array: ", randomList)
print("Size of array: ", len(randomList))
print("Sum of objects in array: ", sum(randomList))
print(sum(randomList))
sumOfSlaves = sumOfSlaves + sum(randomList)
return ()
else:
for i in range(n):
randomList = [(randint(0, 1000)) for i in range(sizeOfArray)]
print("Objects in array: ", randomList)
print("Size of array: ", len(randomList))
print("Sum of objects in array: ", sum(randomList))
sumOfSlaves = sumOfSlaves + sum(randomList)
print("Aggregate of slave threads so far: ", sumOfSlaves)
return ()
if __name__ == "__main__":
o = int(input("please enter how many slave processor you need: "))
x = threading.Thread(target=master_thread, args=(o,))
logging.info("Main : before running thread")
x.start()
This works and compiles just like the previous block of code but I still need to make the internals into mini threads. How can I go about doing this?