Change language

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 

Shop

Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best laptop for development

$499+
Gifts for programmers

Best laptop for Cricut Maker

$299+
Gifts for programmers

Best laptop for hacking

$890
Gifts for programmers

Best laptop for Machine Learning

$699+
Gifts for programmers

Raspberry Pi robot kit

$150

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically