Example #1
0
 def test_nice(self):
     handle = win32api.OpenProcess(win32con.PROCESS_QUERY_INFORMATION,
                                   win32con.FALSE, os.getpid())
     self.addCleanup(win32api.CloseHandle, handle)
     sys_value = win32process.GetPriorityClass(handle)
     psutil_value = psutil.Process().nice()
     self.assertEqual(psutil_value, sys_value)
Example #2
0
def new_priority(priority=1):
    """Context manager to execute a with clause as a different priority."""
    pid = win32api.GetCurrentProcessId()
    handle = win32api.OpenProcess(win32con.PROCESS_ALL_ACCESS, True, pid)
    original_priority = win32process.GetPriorityClass(handle)
    win32process.SetPriorityClass(handle, priorityclasses[priority])
    try:
        yield  # execute with-statement here
    finally:
        win32process.SetPriorityClass(handle, original_priority)
Example #3
0
 def getProcessPriorityByPID(self,pid):
     pClass = -1
     if (isinstance(pClass,int)):
         pHand = win32api.OpenProcess(win32con.PROCESS_QUERY_INFORMATION, 0, pid)
         if (pHand):
             pClass = win32process.GetPriorityClass(pHand)
         else:
             print >>sys.stderr, '(%s).WARNING :: Unable to get the process handler for PID of "%s".' % (ObjectTypeName.objectSignature(self),pid)
         win32api.CloseHandle(pHand)
     else:
         print >>sys.stderr, '(%s).ERROR :: Unable to determine how to handle the pClass of "%s" which is of type "%s".' % (ObjectTypeName.objectSignature(self),pClass,type(pClass))
     return pClass
Example #4
0
 def processPriority(priority=1):
     """Context manager to execute a with clause as a different priority."""        
     priorityclasses = [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]
     pid = win32api.GetCurrentProcessId()
     handle = win32api.OpenProcess(win32con.PROCESS_ALL_ACCESS, True, pid)
     original_priority = win32process.GetPriorityClass(handle)
     win32process.SetPriorityClass(handle, priorityclasses[priority])
     try:
         yield # execute with-statement here
     finally:
         win32process.SetPriorityClass(handle, original_priority)
Example #5
0
def _set_win_process_priority() -> Optional[bool]:
    """
    Sets the process priority class to an elevated value.

    Microbenchmarks are typically very short in duration and therefore are prone
    to noise from other code running on the same machine. Setting the process priority
    to an elevated level while running the benchmarks helps mitigate that noise
    so the results are more accurate (and also more consistent between runs).

    Returns
    -------
    success : bool, optional
        Indication of whether (or not) setting the process priority class succeeded.
        If the priority did not need to be elevated (because it was already), None is returned.
    """
    import win32api, win32process

    # Psuedo-handle for the current process.
    # Because this is a psuedo-handle (i.e. isn't a real handle), it doesn't need to be cleaned up.
    curr_proc_hnd = win32api.GetCurrentProcess()

    # We use the 'ABOVE_NORMAL_PRIORITY_CLASS' here, as that should be good enough to reduce general noise;
    # if necessary, we can try the 'HIGH_PRIORITY_CLASS' but that class and higher can begin to cause the system
    # to become unresponsive so we'll avoid it unless needed; or we can control it with something like a
    # 'strong_hint' bool parameter which chooses between ABOVE_NORMAL_PRIORITY_CLASS and HIGH_PRIORITY.
    target_priority_class: int = win32process.ABOVE_NORMAL_PRIORITY_CLASS

    try:
        # Get the current process priority class. If it's already equal to or higher than the class
        # we were going to set to, don't bother -- we don't want to lower it.
        current_priority_class = win32process.GetPriorityClass(curr_proc_hnd)
        if current_priority_class >= target_priority_class:
            return None

        else:
            # Try to set the priority level for the current process.
            # It can fail if the user (or process) hasn't been granted the PROCESS_SET_INFORMATION right.
            # https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-setpriorityclass
            return win32process.SetPriorityClass(curr_proc_hnd, target_priority_class)
    except:
        return False
        return 'HIGH'
    elif pClass == win32process.NORMAL_PRIORITY_CLASS:
        return 'NORMAL'
    elif pClass == win32process.IDLE_PRIORITY_CLASS:
        return 'IDLE'
    elif pClass == win32process.REALTIME_PRIORITY_CLASS:
        return 'REAL'
    elif pClass == win32process.ABOVE_NORMAL_PRIORITY_CLASS:
        return 'ABOVE NORMAL'
    elif pClass == win32process.BELOW_NORMAL_PRIORITY_CLASS:
        return 'BELOW NORMAL'


id = win32api.GetCurrentProcessId()
h = win32api.OpenProcess(win32con.PROCESS_ALL_ACCESS, 0, id)
oldPClass = win32process.GetPriorityClass(h)

if displayResults:
    print 'Current Process Priority = ', GetPriorityClassName(oldPClass)

if oldPClass == desiredPClass:
    if displayResults:
        print 'WARNING: Current Process already has desired priority!'
else:
    win32process.SetPriorityClass(h, desiredPClass)
    newPClass = win32process.GetPriorityClass(h)
    if oldPClass == newPClass:
        print 'WARNING: Change of process priority failed!'
    else:
        if displayResults:
            print 'New Process Priority     = ', GetPriorityClassName(