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)
您可以使用psutil模块。
在POSIX平台上:
在Windows上:
在每个类Unix平台(包括Linux和MacOsX)上,请参见
os.nice
here:因为你已经有了一个Windows的配方,它涵盖了大多数平台——调用os.nice,除了Windows之外,在任何地方都有一个正面的参数,在那里使用这个配方。没有“打包好的”跨平台解决方案AFAIK(很难打包这个组合,但是,仅仅打包它会带来多大的额外价值呢?-)
下面是我用来将流程设置为低于正常优先级的解决方案:
lowpriority.py
在Windows和Linux上的Python2.6上测试。
相关问题 更多 >
编程相关推荐