獲取本進程id: os.getpid() spa
或: code
from multiprocessing import current_process
current_process().pid
獲取父進程id: os.getppid()blog
from multiprocessing import Process import time import os class MyProcess(Process): # 繼承Process類 def __init__(self, name): super().__init__() self.name = name def run(self): # 必須重寫run方法 print('%s is running; 父進程id是:%s' % (os.getpid(), os.getppid())) time.sleep(3) print('%s is ending; 父進程id是:%s' % (os.getpid(), os.getppid())) if __name__ == '__main__': p = MyProcess('xxx') p.start() # start自動綁定到run方法 print('主進程ID是:%s; 主進程的父進程ID是: %s' % (os.getpid(), os.getppid())) # 主進程的父進程是pycharm或執行該腳本的進程 # 輸出結果: # 主進程ID是:771; 主進程的父進程ID是: 540 # 772 is running; 父進程id是:771 # 772 is ending; 父進程id是:771