I am trying to understand how 2 methods can be executed in parallel using a simple and dumbed down example. The issue is with respect to the execution results. I observe that the execution happens in sequential order where the method threeCheck()
is executed in its entirety before sevenCheck()
is even called.
But my understanding was that both the methods would be executed in parallel.
threeCheck()
is a method that checks if a number is divisible by 3sevenCheck()
is a method that checks if a number is divisible by 7- I have added a random sleep duration in the method calls to mimic latency.
I see that the inputs passed into threeCheck()
Method are being processed in parallel. If we follow sequence, the numbers in the array divisible by 3 are :
84, 66, 60, 66, 87, 99, 42, .......
But when we observe the execution results, the sequence shows otherwise for these first results
import multiprocessing as mp
import time
import random
def threeCheck(inputParam):
if inputParam % 3 == 0:
duration = random.randint(0,6)
print("3 INPUT = {0} and Sleep = {1}".format(inputParam, duration))
time.sleep(duration)
return inputParam
def sevenCheck(inputParam):
if inputParam % 7 == 0:
duration = random.randint(0,6)
print("7 INPUT = {0} and Sleep = {1}".format(inputParam, duration))
time.sleep(duration)
return inputParam
def main():
cpuCount = mp.cpu_count()
pool = mp.Pool(cpuCount)
inputValues = []
for i in range(50):
inputValues.append(random.randint(0,100))
resultThree = pool.map(threeCheck, inputValues)
resultSeven = pool.map(sevenCheck, inputValues)
print("INPUT = ", inputValues)
print("CPU = ", cpuCount)
print("RESULT 3 = ", resultThree)
print("RESULT 7 = ", resultSeven)
if __name__ == "__main__":
main()
OUTPUT:
3 INPUT = 84 and Sleep = 5
3 INPUT = 66 and Sleep = 2
3 INPUT = 60 and Sleep = 1
3 INPUT = 99 and Sleep = 3
3 INPUT = 66 and Sleep = 3
3 INPUT = 42 and Sleep = 5
3 INPUT = 12 and Sleep = 1
3 INPUT = 87 and Sleep = 6
3 INPUT = 57 and Sleep = 5
3 INPUT = 42 and Sleep = 3
3 INPUT = 30 and Sleep = 2
3 INPUT = 63 and Sleep = 0
3 INPUT = 42 and Sleep = 6
3 INPUT = 24 and Sleep = 2
3 INPUT = 21 and Sleep = 6
3 INPUT = 30 and Sleep = 4
3 INPUT = 27 and Sleep = 0
7 INPUT = 7 and Sleep = 1
7 INPUT = 84 and Sleep = 6
7 INPUT = 42 and Sleep = 6
7 INPUT = 28 and Sleep = 1
7 INPUT = 21 and Sleep = 6
7 INPUT = 42 and Sleep = 5
7 INPUT = 63 and Sleep = 3
7 INPUT = 91 and Sleep = 5
7 INPUT = 42 and Sleep = 6
INPUT = [7, 95, 25, 19, 59, 82, 47, 68, 20, 76, 84, 8, 88, 29, 1, 66, 60, 66, 87, 25, 23, 80, 52, 99, 65, 65, 25, 42, 12, 38, 28, 88, 57, 21, 27, 40, 42, 38, 31, 91, 30, 68, 22, 24, 63, 42, 64, 17, 65, 30]
CPU = 4
RESULT 3 = [None, None, None, None, None, None, None, None, None, None, 84, None, None, None, None, 66, 60, 66, 87, None, None, None, None, 99, None, None, None, 42, 12, None, None, None, 57, 21, 27, None, 42, None, None, None, 30, None, None, 24, 63, 42, None, None, None, 30]
...
I am sure that I am missing something here ..