Python - Reduces Value

Using python, I can easily increase the current subtlety of the process:

>>> import os
>>> import psutil

>>> # Use os to increase by 3
>>> os.nice(3)
3

>>> # Use psutil to set to 10
>>> psutil.Process(os.getpid()).nice(10)
>>> psutil.Process(os.getpid()).nice()
10

However, reducing the attractiveness of the process is not possible:

>>> os.nice(-1)
OSError: [Errno 1] Operation not permitted

>>> psutil.Process(os.getpid()).nice(5)
psutil.AccessDenied: psutil.AccessDenied (pid=14955)

What is the right way to do this? And is the ratchet mechanism a mistake or function?

+6
source share
3 answers

Linux by default does not allow unprivileged users to reduce the pleasant value (i.e., increase the priority) of their processes, so that one user does not create a high-priority process to expel other users. Python simply redirects the error that the OS provides you as an exception.

root , root .

+7

Python os.nice. man 2 nice , :

nice() inc . ( nice value .) . getpriority (2).

+3

I had the same error [Errno 2] Operation not permitted.

I do not want to start my script with sudo, so I found the following way:

def decrease_nice():
    pid = os.getpid()
    os.system("sudo renice -n -19 -p " + str(pid))

def normal_nice():
    pid = os.getpid()
    os.system("sudo renice -n 0 -p " + str(pid))
0
source

Source: https://habr.com/ru/post/1619524/


All Articles