I have developed a function based on several examples that I have seen on different websites.
It can be used interchangeably if you work on Windows or on Linux.
In addition, I have implemented the option of being able to establish the priority level through the parameters passed to the function, so that not only can the priority be reduced, but also increased. The default value of the parameters is that which corresponds to "normal" priority
import win32api
import win32process
import win32con
import sys
import os
def set_priority(win_n=2, py_n=0):
"""
Establece la prioridad del proceso.
Es independiente del sistema operativo.
Parameters
----------
win_n: int
Determina el nivel de prioridad del proceso.
py_n: int
Determina el nivel de prioridad del proceso.
"""
#
# Determinamos el sistema operativo y la version.
try:
sys.getwindowsversion()
except AttributeError:
isWindows = False
else:
isWindows = True
#
if isWindows:
# Almacenamos los diferentes niveles de prioridad en una lista.
prior_classes = [win32process.IDLE_PRIORITY_CLASS,
win32process.BELOW_NORMAL_PRIORITY_CLASS,
win32process.NORMAL_PRIORITY_CLASS,
win32process.ABOVE_NORMAL_PRIORITY_CLASS,
win32process.HIGH_PRIORITY_CLASS,
win32process.REALTIME_PRIORITY_CLASS]
# Obtenemos el identificador del proceso, del proceso de llamada.
# https://msdn.microsoft.com/en-us/library/windows/desktop/ms683180(v=vs.85).aspx
pid = win32api.GetCurrentProcessId()
# Abre un objeto de un proceso local existente.
# https://msdn.microsoft.com/es-es/library/windows/desktop/ms684320(v=vs.85).aspx
handle = win32api.OpenProcess(win32con.PROCESS_ALL_ACCESS,
True, pid)
# Establecemos la prioridad del proceso.
# https://msdn.microsoft.com/es-es/library/windows/desktop/ms686219(v=vs.85).aspx
win32process.SetPriorityClass(handle, prior_classes(win_n))
#
else:
os.nice(py_n)
EDIT:
Attached link to a website with other similar examples ,