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

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程