0

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?

  • 1
    i hope it's not too late to say that python has a Global Interpreter Lock (GIL) that prevents threads from executing python concurrently, so adding threads will only get your code slower. – Ahmed AEK Mar 14 '23 at 18:17
  • Can you explain the rationale behind this? The magnitude of what you're doing here is so small that sequential/linear processing will almost certainly be more efficient than any multithreading or multiprocessing approach. BTW - As a general rule, multithreading is **not** well-suited to CPU intensive parallelism (due to the GIL alluded to in a previous comment). That's where multiprocessing comes into play but even that isn't appropriate in this case because the overheads would outweigh the size of this problem – DarkKnight Mar 14 '23 at 18:30
  • I don't think there's any rationale really its just to increase my knowledge of multi-threading, but I don't know how to do this. – QRebourneQ Mar 14 '23 at 18:39
  • 1
    Please trim your code to make it easier to find your problem. Follow these guidelines to create a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Blue Robin Mar 15 '23 at 00:02

0 Answers0