+

Python | os.nice () method

All functions in the os module raise OSError for invalid or unreachable file names and paths or other arguments that are of the correct type but are not accepted by the operating system.

os.nice () in Python is used to increase the precision of a process by a specified value.

Good or nice value — it is a set of recommendations for the CPU when a process wants to get the CPU time to do its job. The Accuracy of the process ranges from -20 to 19 (both inclusive). A process with a lower value good or good gets higher priority and more CPU time, while a process with a higher value good gets lower priority and less CPU time.

Note: os.nice () is only available on UNIX platforms. Any user can improve the accuracy of the process, but only the superuser can reduce the efficiency of the process. 
Superuser — this is a root user or administrator who has full rights to run or run 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: Using the os.nice () method to improve the quality of the process

# Python program to explain the os.nice () method

 
# import of the os module

import os

  

  
# Get the current nice value
Process #

niceValue = os.nice ( 0 )

 
# Print the current nice value
Process #

print ( "Current nice value of the process:" , niceValue)

 
# Increase grace
Process #

value = 5

niceValue = os.nice (value)

print ( "Niceness of the process increased" )

 
# Print the current nice value
Process #

print ( "Current nice value of the process:" , niceValue)

 

 
# Increase favor
Process #

value = 10

niceValue = os.nice (value)

print ( " Niceness of the process increased " )

 
# Print the current nice value
Process #

print ( " Current nice value of the process: " , niceValue)

  

 
# Increase favor
Process #

value = 10

niceValue = os.nice (value )

print ( "Niceness of the process increased " )

  
# Print the current nice value
Process #

print ( "Current nice value of the process:" , niceValue)

 

 
# The most attractive process possible
# can be 19, so if the value to be set is greater than
# maximum limit, it will be set to
# maximum mercy ie. 19

Exit:

 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: Using the os.nice () method to make the process less attractive

# Python program to explain the os. nice ()

 
# import of the os module

import os

 

 
# Note: only the superuser can
# reduce the attractiveness of the process
# so run the program as superuser
# to avoid permission error

 
# Get the current nice value
Process #

niceValue = os.nice ( 0 )

 
# Print the current nice value
Process #

print ( "Current nice value of the process: " , niceValue)

  
# Decrease favor
Process #

value = - 5

niceValue = os.nice (value)

print ( "Niceness of the process decreased" )

 
# Print current nice value
Process #

print ( "Current nice value of the process: " , niceValue)

  
# Decrease grace
# of the current process

value = - 10

niceValue = os.nice (value)

print ( "Niceness of the process decreased" )

 
# Print the current nice value
Process #

print ( "Current nice value of the process:" , niceValue)

 

 
# Decrease grace
# of the current process

value = - 15

niceValue = os.nice (value)

print ( "Niceness of the process decreased" )

 
# Print current nice value
Process #

print ( "Current nice value of the process:" , niceValue)

 

  
# Minimum attractiveness of the process
# can be -20, so if you need to set
# below the minimum limit
  # will be set to the minimum possible
# mercy i.e. -20

Exit:

 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                                        
             
                
Get Solution for free from DataCamp guru