Open In App

Python | os.nice() method

Last Updated : 27 Jun, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

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




# 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
Code #2: Use of os.nice() method to decrease the process’s 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


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
Previous
Next
Share your thoughts in the comments

Similar Reads