Sunday 20 December 2015

Python: multiprocessing: Determining current process


'multiprocessing.current_process()' is used to get the reference of current running process.
from multiprocessing import Process
import multiprocessing
import os

def print_data():
    name = multiprocessing.current_process().name
    id = multiprocessing.current_process().pid
    print(name, id)
    print("Parent process Id : ",os.getppid())

if __name__=="__main__":
    proc1 = Process(name='process1', target=print_data)
    proc2 = Process(name='process2', target=print_data)
    proc3 = Process(name='process3', target=print_data)

    proc1.start()
    proc2.start()
    proc3.start()

    proc1.join()
    proc2.join()
    proc3.join()

    print("Finished")


Output
process1 29889
Parent process Id :  29888
process2 29890
Parent process Id :  29888
process3 29891
Parent process Id :  29888
Finished

Observe the output, all the three processes has same parent process id.




Previous                                                 Next                                                 Home

No comments:

Post a Comment