Python os.nice()
Python os模块中的所有函数在文件名和路径无效或不可访问,或其他具有正确类型但操作系统不接受的参数时都会引发OSError。
Python中的os.nice()方法用于按指定值增加进程的niceness。
当进程想要获得CPU时间来执行它的工作时,nice或nice值是CPU要遵循的一组准则。工艺精细度范围在-20至19之间(包括两者)。nice值较低的进程会被赋予更高的优先级和更多的CPU时间,而nice值较高的进程会被赋予更低的优先级和更少的CPU时间。
注意:os.nice()方法只在UNIX平台上可用。任何用户都可以提高进程的良好性,但只有超级用户才能降低进程的良好性。
超级用户是指具有运行或执行操作系统中任何程序的所有权限的根用户或管理用户。
语法:os.nice(value)
参数:
value:整型值。
过程的新精细度将计算为新精细度=以前精细度+指定值
返回类型:该方法返回一个整数值,表示进程的新优点。
示例1
使用os.nice()方法来提高进程的niceness
# Python program to explain os.nice() method
# importing os module
import os
# Get the current nice value
# of the process
niceValue = os.nice(0)
# Print the current nice value
# of the process
print("Current nice value of the process:", niceValue)
# Increase the niceness
# of the process
value = 5
niceValue = os.nice(value)
print("\nNiceness of the process increased")
# Print the current nice value
# of the process
print("\nCurrent nice value of the process:", niceValue)
# Increase the niceness
# of the process
value = 10
niceValue = os.nice(value)
print("\nNiceness of the process increased")
# Print the current nice value
# of the process
print("\nCurrent nice value of the process:", niceValue)
# Increase the niceness
# of the process
value = 10
niceValue = os.nice(value)
print("\nNiceness of the process increased")
# Print the current nice value
# of the process
print("\nCurrent nice value of the process:", niceValue)
# The maximum possible niceness of a process
# can be 19 so if niceness to be set exceeds
# the maximum limit then it will set to
# the maximum possible niceness i.e 19
Output:
Current nice value of the process: 0
Niceness of the process increased
Current nice value of the process: 5
Niceness of the process increased
Current nice value of the process: 15
Niceness of the process increased
Current nice value of the process: 19
示例2
使用os.nice()方法来降低进程的niceness
# Python program to explain os.nice() method
# importing os module
import os
# Note : Only a superuser can
# decrease a process's niceness
# so run the program as superuser
# to avoid permission related error
# Get the current nice value
# of the process
niceValue = os.nice(0)
# Print the Current nice value
# of the process
print("Current nice value of the process:", niceValue)
# Decrease the niceness
# of the process
value = -5
niceValue = os.nice(value)
print("\nNiceness of the process decreased")
# Print the current nice value
# of the process
print("\nCurrent nice value of the process:", niceValue)
# Decrease the niceness
# of the current process
value = -10
niceValue = os.nice(value)
print("\nNiceness of the process decreased")
# Print the current nice value
# of the process
print("\nCurrent nice value of the process:", niceValue)
# Decrease the niceness
# of the current process
value = -15
niceValue = os.nice(value)
print("\nNiceness of the process decreased")
# Print the current nice value
# of the process
print("\nCurrent nice value of the process:", niceValue)
# The minimum possible niceness of a process
# can be -20 so if niceness to be set is
# lower than the minimum limit then
# it will set to the minimum possible
# niceness i.e -20
输出:
Current nice value of the process: 0
Niceness of the process decreased
Current nice value of the process: -5
Niceness of the process decreased
Current nice value of the process: -15
Niceness of the process decreased
Current nice value of the process: -20