Python os._exit()方法
Python中的OS模块提供了与操作系统交互的函数。OS属于Python的标准实用模块。此模块提供了一种使用操作系统相关功能的可移植方式。 os._exit() 方法用于退出具有指定状态的进程,而不调用清理处理程序、刷新stdio缓冲区等。注意:此方法通常用于os.fork()系统调用后的子进程。退出进程的标准方法是 sys.exit (n) 方法.
os._exit 语法
os._exit(status)
os._exit 参数
status:一个整数值或以上的定义值,表示退出状态。
返回类型:该方法在调用进程中不返回任何值。
os._exit 示例1
使用 os._exit()方法
# Python program to explain os._exit() method
# importing os module
import os
# Create a child process
# using os.fork() method
pid = os.fork()
# pid greater than 0
# indicates the parent process
if pid > 0:
print("\nIn parent process")
# Wait for the completion
# of child process and
# get its pid and
# exit status indication using
# os.wait() method
info = os.waitpid(pid, 0)
# os.waitpid() method returns a tuple
# first attribute represents child's pid
# while second one represents
# exit status indication
# Get the Exit code
# used by the child process
# in os._exit() method
# firstly check if
# os.WIFEXITED() is True or not
if os.WIFEXITED(info[1]) :
code = os.WEXITSTATUS(info[1])
print("Child's exit code:", code)
else :
print("In child process")
print("Process ID:", os.getpid())
print("Hello ! Geeks")
print("Child exiting..")
# Exit with status os.EX_OK
# using os._exit() method
# The value of os.EX_OK is 0
os._exit(os.EX_OK)
输出:
In child process
Process ID: 15240
Hello! Geeks
Child exiting..
In parent process
Child's exit code: 0