Commands equivalent to 'nice' and 'cpulimit' for windows 7?

0

I want to limit the access of my programs to the resources of the team to prevent it from being hung up so that I can continue working on other issues while the programs are running in the background. I'm looking for a generic way to do it regardless of how my scripts are. I have seen that for those who work on Linux there are the options nice and cpulimit , but I work on win7 (change to Linux is not an option since I am not the one who decides the configuration of the pc I use).

What options do I have working on windows to achieve a result equivalent to nice and cpulimit on win7?

    
asked by Zhisi 04.06.2018 в 16:52
source

1 answer

0

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 ,

    
answered by 06.06.2018 / 16:33
source