Python | os.nice() method
Last Updated :
27 Jun, 2019
OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality.
All functions in os module raise OSError in the case of invalid or inaccessible file names and paths, or other arguments that have the correct type, but are not accepted by the operating system.
os.nice()
method in Python is used to increment the process’s niceness by specified value.
Niceness or nice value is set of guidelines for the CPU to follow when a process wants to get CPU time in order to executes its job. The niceness of process range between -20 to 19 (both inclusive). A process with a lower niceness or nice value is given higher priority and more CPU time while a process with a higher nice value is given a lower priority and less CPU time.
Note: os.nice()
method is only available on UNIX platforms. Any user can increase the niceness of the process but only a superuser can decrease the niceness of a process.
Superuser means a root user or an administrative user who has all the permissions to run or execute any program in the operating system.
Syntax: os.nice(value)
Parameters:
value: An integer value.
The new niceness of the process will be calculated as new niceness = previous niceness + specified value
Return Type: This method returns an integer value which represents the new niceness of the process.
Code #1: Use of os.nice() method to increase the process’s niceness
import os
niceValue = os.nice( 0 )
print ( "Current nice value of the process:" , niceValue)
value = 5
niceValue = os.nice(value)
print ( "\nNiceness of the process increased" )
print ( "\nCurrent nice value of the process:" , niceValue)
value = 10
niceValue = os.nice(value)
print ( "\nNiceness of the process increased" )
print ( "\nCurrent nice value of the process:" , niceValue)
value = 10
niceValue = os.nice(value)
print ( "\nNiceness of the process increased" )
print ( "\nCurrent nice value of the process:" , niceValue)
|
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
Code #2: Use of os.nice() method to decrease the process’s niceness
import os
niceValue = os.nice( 0 )
print ( "Current nice value of the process:" , niceValue)
value = - 5
niceValue = os.nice(value)
print ( "\nNiceness of the process decreased" )
print ( "\nCurrent nice value of the process:" , niceValue)
value = - 10
niceValue = os.nice(value)
print ( "\nNiceness of the process decreased" )
print ( "\nCurrent nice value of the process:" , niceValue)
value = - 15
niceValue = os.nice(value)
print ( "\nNiceness of the process decreased" )
print ( "\nCurrent nice value of the process:" , niceValue)
|
Output:
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
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...