2

I know that os.nice(20) sets the maximum nice level on linux.

But I don't know the value of other unixes (I don't care about MS-Win).

Is there somewhere a symbolic value for the maximal nice value which I can use from python?

Prefered solution:

os.nice(NICE_MAX)

I tried to find a solution here:

guettli
  • 25,042
  • 81
  • 346
  • 663

2 Answers2

2

I think the closest to a standard we get here is from POSIX (IEEE 1003). The actual specification there does leave the range to the implementation, but notes:

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.

Thus, if you just want the highest niceness, you could pass it INT_MAX, but that value is not easily found in Python. NZERO does provide the bounds, but is likewise hard to find (it's defined in limits.h).

However, Python's os.nice doesn't behave like setpriority - it increases the process niceness, and also returns the resulting niceness. So if all you want to do is set the highest niceness, you could just run a while loop until os.nice(N) returns the same value twice, given N is any positive value that does fit in ctypes.c_int (any integer from 1 to 32767 is guaranteed to fit).

Yann Vernier
  • 15,414
  • 2
  • 28
  • 26
0

From getpriority(2) notes:

The actual priority range varies between kernel versions. Linux before 1.3.36 had -infinity..15. Since kernel 1.3.43, Linux has the range -20..19. Within the kernel, nice values are actually represented using the corresponding range 40..1 (since negative numbers are error codes) and these are the values employed by the setpriority() and getpriority() system calls. The glibc wrapper functions for these system calls handle the translations between the user-land and kernel representations of the nice value according to the formula unice = 20 - knice.

pbacterio
  • 1,094
  • 6
  • 12