📜  Python| os.nice() 方法

📅  最后修改于: 2022-05-13 01:54:41.302000             🧑  作者: Mango

Python| os.nice() 方法

Python中的OS 模块提供了与操作系统交互的功能。操作系统属于 Python 的标准实用程序模块。该模块提供了一种使用操作系统相关功能的可移植方式。

os 模块中的所有函数在文件名和路径无效或不可访问的情况下,或具有正确类型但操作系统不接受的其他参数的情况下引发OSError

Python中的os.nice()方法用于通过指定值增加进程的 niceness。

Nicenessnice value是 CPU 在进程想要获得 CPU 时间以执行其工作时遵循的一组准则。进程的好坏范围在 -20 到 19 之间(包括两者)。具有较低nicenessnice 值的进程被赋予更高的优先级和更多的 CPU 时间,而具有更高nice 值的进程被赋予较低的优先级和更少的 CPU 时间。

注意: os.nice()方法仅在 UNIX 平台上可用。任何用户都可以增加进程的友好度,但只有超级用户可以降低进程的友好度。
超级用户是指具有在操作系统中运行或执行任何程序的所有权限的 root 用户或管理用户。

代码#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 
输出:
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