Skip to main content
 首页 » 编程设计

python之os.nice() 的最大 nice 值

2024年11月01日9yxwkf

我知道 os.nice(20) 设置了 linux 上的最大 nice 级别。

但我不知道其他 unix 的值(value)(我不关心 MS-Win)。

我可以在 python 中使用最大 nice 值的某个符号值吗?

首选解决方案:

os.nice(NICE_MAX)

我试图在这里找到解决方案:

请您参考如下方法:

我认为我们在这里获得的最接近标准的是 POSIX (IEEE 1003)。那里的实际规范确实将范围留给了实现,但注意:

If value+{NZERO} is less than the system’s lowest supported nice value, setpriority( ) shall set the nice value to the lowest supported value; if value+{NZERO} is greater than the system’s highest supported nice value, setpriority( ) shall set the nice value to the highest supported value.

因此,如果您只想要最高的 niceness,您可以将其传递给 INT_MAX,但在 Python 中不容易找到该值。 NZERO 确实提供了边界,但同样很难找到(它在 limits.h 中定义)。

但是,Python 的 os.nice不像setpriority - 它增加 过程的友好度,并返回结果的友好度。所以如果你想做的只是设置最高的 niceness,你可以运行一个 while 循环直到 os.nice(N) 返回相同的值两次,给定 N 是任何适合的正值ctypes.c_int(保证适合从 1 到 32767 的任何整数)。