1. 程式人生 > >5.1.3 網絡編程進階---查看進程ID和父進程ID

5.1.3 網絡編程進階---查看進程ID和父進程ID

父進程 elf pycharm __init__ div run 網絡編程 %s 輸出

獲取本進程id: os.getpid()

獲取父進程id: os.getppid()

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

5.1.3 網絡編程進階---查看進程ID和父進程ID