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
相關用法
- Python set()用法及代碼示例
- Python next()用法及代碼示例
- Python os.dup()用法及代碼示例
- Python getattr()用法及代碼示例
- Python os.getpgrp()用法及代碼示例
- Python os.fork()用法及代碼示例
- Python os.getsid()用法及代碼示例
- Python os.setregid()用法及代碼示例
- Python os.pwrite()用法及代碼示例
- Python os.writev()用法及代碼示例
- Python os.readv()用法及代碼示例
- Python sympy.det()用法及代碼示例
- Python PIL ImageOps.fit()用法及代碼示例
- Python os.readlink()用法及代碼示例
注:本文由純淨天空篩選整理自ihritik大神的英文原創作品 Python | os.nice() method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。