I'm now learning multiprocess library in python, so I design some experiments to see how does the overall process proceed. I'm confused about the experiment below. The first question is why the value of link_id that each subprocess uses is still 0. The second question is why line3-line6 has been conducted many times (9 times in my case), and it is not equal to the number of subprocesses I have. Python version: 3.7.4, Operation system: win10
from multiprocessing import Pool
link_id = 0
print('head is printing link, value {}, id {}'.format(link_id,id(link_id)))
node_id = 0
print('head is printing node, value {}, id {}'.format(node_id,id(node_id)))
def job(i):
print('job {} is printing link, value {}, var id {}'.format(i,link_id,id(link_id)))
def mainFunc():
p = Pool()
for i in range(20): p.apply_async(job, args=(i,))
p.close()
p.join()
if __name__ == '__main__':
link_id = 10
print('main is printing link, value {}, id {}'.format(link_id,id(link_id)))
mainFunc()
print('main is printing link, value {}, id {}'.format(link_id,id(link_id)))