I am learning about parallel processing in python and I have some very specific doubts regarding the execution flow of the following program. In this program, I am splitting my list into two parts depending on the process. My aim is to run the add function twice parallely where one process takes one part of the list and other takes other part.
import multiprocessing as mp
x = [1,2,3,4]
print('hello')
def add(flag, q_f):
global x
if flag == 1:
dl = x[0:2]
elif flag == 2:
dl = x[2:4]
else:
dl = x
x = [i+2 for i in dl]
print('flag = %d'%flag)
print('1')
print('2')
print(x)
q_f.put(x)
print('Above main')
if __name__ == '__main__':
ctx = mp.get_context('spawn')
print('inside main')
q = ctx.Queue()
jobs = []
for i in range(2):
p = mp.Process(target = add, args = (i+1, q))
jobs.append(p)
for j in jobs:
j.start()
for j in jobs:
j.join()
print('completed')
print(q.get())
print(q.get())
print('outside main')
The output which I got is
hello
Above main
outside main
flag = 1
1
2
[3, 4]
hello
Above main
outside main
flag = 2
1
2
[5, 6]
hello
Above main
inside main
completed
[3, 4]
[5, 6]
outside main
My questions are
1) From the output, we can see that one process is getting executed first, then the other. Is the program actually utilizing multiple processors for parallel processing? If not, how can I make it parallely process? If it was parallely processing, the print statements print('1') print('2') should be executed at random, right?
2) Can I check programmatically on which processor is the program running?
3) Why are the print statements outside main(hello, above main, outside main) getting executed thrice?
4) What is the flow of the program execution?