Change process priority in Python, cross-platform

I’ve got a Python program that does time-consuming computations. Since it uses high CPU, and I want my system to remain responsive, I’d like the program to change its priority to below-normal.

I found this:
Set Process Priority In Windows – ActiveState

But I’m looking for a cross-platform solution.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Here’s the solution I’m using to set my process to below-normal priority:

lowpriority.py

def lowpriority():
    """ Set the priority of the process to below-normal."""

    import sys
    try:
        sys.getwindowsversion()
    except AttributeError:
        isWindows = False
    else:
        isWindows = True

    if isWindows:
        # Based on:
        #   "Recipe 496767: Set Process Priority In Windows" on ActiveState
        #   http://code.activestate.com/recipes/496767/
        import win32api,win32process,win32con

        pid = win32api.GetCurrentProcessId()
        handle = win32api.OpenProcess(win32con.PROCESS_ALL_ACCESS, True, pid)
        win32process.SetPriorityClass(handle, win32process.BELOW_NORMAL_PRIORITY_CLASS)
    else:
        import os

        os.nice(1)

Tested on Python 2.6 on Windows and Linux.

Method 2

You can use psutil module.

On POSIX platforms:

>>> import psutil, os
>>> p = psutil.Process(os.getpid())
>>> p.nice()
0
>>> p.nice(10)  # set
>>> p.nice()
10

On Windows:

>>> p.nice(psutil.HIGH_PRIORITY_CLASS)

Method 3

On every Unix-like platform (including Linux and MacOsX), see os.nice here:

os.nice(increment)
Add increment to the process’s “niceness”. Return the new niceness. Availability: Unix.

Since you already have a recipe for Windows, that covers most platforms — call os.nice with a positive argument everywhere but Windows, use that recipe there. There is no “nicely packaged” cross-platform solution AFAIK (would be hard to package this combo up, but, how much extra value would you see in just packaging it?-)

Method 4

If you don’t have access to some of these modules you can potentially do it in Windows with:

import os  
def lowpriority():  
    """ Set the priority of the process to below-normal."""  
    os.system("wmic process where processid=""+str(os.getpid())+"" CALL   setpriority "below normal"")  

You can obviously distinguish OS types as with examples above for compatibility.

Method 5

Here a code that work good for me:

import os
import psutil

os_used = sys.platform
process = psutil.Process(os.getpid())  # Set highest priority for the python script for the CPU
if os_used == "win32":  # Windows (either 32-bit or 64-bit)
    process.nice(psutil.REALTIME_PRIORITY_CLASS)
elif os_used == "linux":  # linux
    process.nice(psutil.IOPRIO_HIGH)
else:  # MAC OS X or other
    process.nice(20)

For futher informations on the number and constant used in the code the full documentation is here:
https://psutil.readthedocs.io/en/latest/#


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x