This os.nice()
method is used to add increments in the process's
It ranges between -20 to +19, inclusive of boundary values. The higher the nice value, the lesser the priority and CPU time, and vice versa.
Note: Root or Superuser has privileges to change process niceness.
os.nice(increment)
It takes the following argument value.
increment
: This is an integer value between -20
and +19
, where -20
is the maximum and +19
is the minimum. It is calculated in different ways depending upon system architecture. In UNIX based systems, it is calculated as: new_niceness = old_niceness + specified_value
.
It returns process niceness or nice value as an integer.
In this code snippet, we'll use the os.nice()
method and its different variants.
# importing os and random moduleimport osimport random# current nice value of the process# set to random valueniceness = os.nice(random.randint(1,20))# show current niceness valueprint("Current niceness value:", niceness)# set to default nicenessniceness = os.nice(0)# show current niceness valueprint("Current niceness value:", niceness)
os.nice()
with a random argument value between 1
and 20
. In line 8, we print the updated value to the console.os.nice()
at 0
, which is also considered a default value. In line 12, we print the updated value to the console.