在Python中跨平台更改流程优先级

时间:2022-06-16 02:59:55

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.

我有一个Python程序,可以进行耗时的计算。由于它使用高CPU,并且我希望我的系统保持响应,我希望程序将其优先级更改为低于正常值。

I found this: Set Process Priority In Windows - ActiveState

我发现了这一点:在Windows中设置进程优先级 - ActiveState

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

但我正在寻找一个跨平台的解决方案。

3 个解决方案

#1


31  

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.

在Windows和Linux上测试Python 2.6。

#2


15  

You can use psutil module.

您可以使用psutil模块。

On POSIX platforms:

在POSIX平台上:

>>> 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)

#3


9  

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

在每个类Unix平台(包括Linux和MacOsX)上,请参见os.nice:

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?-)

既然你已经有一个适用于Windows的配方,它涵盖了大多数平台 - 除了Windows之外,在所有地方调用os.nice并使用正面参数,在那里使用那个配方。没有“精心打包”的跨平台解决方案AFAIK(很难打包这个组合,但是,在打包它时你会看到多少额外的价值? - )

#1


31  

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.

在Windows和Linux上测试Python 2.6。

#2


15  

You can use psutil module.

您可以使用psutil模块。

On POSIX platforms:

在POSIX平台上:

>>> 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)

#3


9  

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

在每个类Unix平台(包括Linux和MacOsX)上,请参见os.nice:

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?-)

既然你已经有一个适用于Windows的配方,它涵盖了大多数平台 - 除了Windows之外,在所有地方调用os.nice并使用正面参数,在那里使用那个配方。没有“精心打包”的跨平台解决方案AFAIK(很难打包这个组合,但是,在打包它时你会看到多少额外的价值? - )