简体   繁体   中英

What will subprocesses do when using multiprocess in python

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)))

Why is value of link_id that each subprocess uses 0 ?

Each subprocess is an instance of the python interpreter, each one will run/compile your script and have a separate reference to the module. __name__ == '__main__' will be False though as this is only True when you run the file directly, so link_id will be 0 for each of these subprocesses

Why is line3-line6 conducted many times

The answer to this is the same as the first, the lines will be run number_of_subprocesses + 1 times (the additional 1 is when you run the script). If you change the number of processes in the pool you should see the number of times the lines are run change

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM