Get this process id:os.getpid ()
Get parent Process Id:os.getppid ()
fromMultiprocessingImportProcessImport TimeImportOSclassMyprocess (Process):#inherit the Process class def __init__(self, name): Super ().__init__() Self.name=namedefRun (self):#You must override the Run method Print('%s is running; the parent process ID is:%s'%(Os.getpid (), Os.getppid ())) Time.sleep (3) Print('%s is ending; the parent process ID is:%s'%(Os.getpid (), Os.getppid ()))if __name__=='__main__': P= Myprocess ('XXX') P.start ()#start automatically binds to the Run method Print('The main process ID is:%s; the parent process ID of the main process is:%s'% (Os.getpid (), Os.getppid ()))#The parent process of the main process is pycharm or the process that executes the script#Output Result:#The main process ID is: 771; the parent process ID of the main process is: 540#772 is running; the parent process ID is: 771#772 is ending; the parent process ID is: 771
5.1.3 Network Programming Advanced---View process ID and parent process ID