# coding:utf-8 import os from multiprocessing import Process class MyProcess(Process): def __init__(self, nam): super().__init__() self.nam = nam # 因為父類里有name屬性了,如果這里還是使用屬性name,會覆蓋父類name的值,我使用了nam def run(self): print("子進程開始.") print("子進程號os.getpid():", os.getpid()) print("子進程名字name:", self.name) print("子進程結束.") if __name__ == '__main__': p = MyProcess("lily") p.start() print("p.name:", p.name) print("p.pid:", p.pid)
執行結果: # p.name: MyProcess-1 # p.pid: 6388 # 子進程開始. # 子進程號os.getpid(): 6388 # 子進程名字name: MyProcess-1 # 子進程結束.
有沒有發現,獲取進程號的方法有兩種:p.pid和os.getpid()
