Python os.getppid() - 获取当前进程的父进程ID

春日樱亭

Python os.getppid()

Python中的os.getppid()方法用于获取当前进程的父进程ID

语法:os.getppid()

参数:不需要

返回类型:该方法返回一个整数值,表示当前进程的父进程ID。这个方法的返回类型是’ int ‘类。

示例1

使用os.getppid()方法

# Python program to explain os.getppid() method 
  
# importing os module 
import os
  
# Get the Parent process ID 
# of the current process
ppid = os.getppid()
  
# Print the Parent process ID 
# of the current process
print("Parent Process ID of current process:", ppid)

输出:

Parent process ID of current process: 7653

示例2

使用os.getppid()方法

# Python program to explain os.getppid() method 
  
# importing os module 
import os
  
  
# Check the process ID 
# of the current process
pid = os.getpid()
print("Process ID of Current process:", pid)
  
  
# Create a child process
try:
    pid = os.fork()
except OSError:
    exit("Could not create a child process")
  
  
# In the child process 
# Check its Parent process ID
# os.getppid() will return 
# the process ID of its parent process
if pid == 0:
     parent = os.getppid()
     print("Parent process ID of child process:", parent)

输出:

Process ID of Current process: 7653
Parent process ID of child process: 7653

版权声明:本页面内容旨在传播知识,为用户自行发布,若有侵权等问题请及时与本网联系,我们将第一时间处理。E-mail:284563525@qq.com

目录[+]

取消
微信二维码
微信二维码
支付宝二维码