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

相关文章:

  • 2022-12-23
  • 2021-09-08
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-06-04
  • 2021-11-27
  • 2021-07-09
猜你喜欢
  • 2021-09-25
  • 2021-10-13
  • 2022-12-23
  • 2021-11-14
  • 2021-10-15
  • 2022-12-23
相关资源
相似解决方案