当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Python os.nice()用法及代码示例


Python中的OS模块提供了与操作系统进行交互的函数。操作系统属于Python的标准实用程序模块。该模块提供了使用依赖于操作系统的函数的便携式方法。

如果文件名和路径无效或无法访问,或者具有正确类型但操作系统不接受的其他参数,则os模块中的所有函数都会引发OSError。

os.nice()Python中的方法用于按指定的值增加流程的美观程度。


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

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

用法: os.nice(value)

参数:
value:一个整数值。
该过程的新舒适度将被计算为新的舒适度=先前的舒适度+指定值

返回类型:此方法返回一个整数值,该整数值表示该过程的新特性。

代码1:使用os.nice()方法来提高流程的美观度
# 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()方法降低流程的美观程度
# 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


相关用法


注:本文由纯净天空筛选整理自ihritik大神的英文原创作品 Python | os.nice() method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。