# 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()