end18 / psutil

Automatically exported from code.google.com/p/psutil
Other
0 stars 0 forks source link

get/set process nice-ness #142

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
= Proposal = 

Add the possibility to get/set the process niceness (priority) as when using 
"nice" and "renice" cmdline utilities.

= Implementation = 

Getting process niceness should be quite easy across all UNIX platforms.
For setting it we need to use setpriority(): 
http://linux.die.net/man/2/setpriority
Being it a POSIX standard we can have a unique C implementation for Linux, OSX 
and FreeBSD we can add in a new psutil/_psutil_posix.c file.
This worries me a little since that would mean that we start to require 
*compiling* psutil on Linux, which is the only platform for which we used a 
100% pure python implementation. We can avoid that by using ctypes module:
http://stackoverflow.com/questions/1920952/linux-group-scheduling-for-user-not-b
eing-applied-to-setuid-ed-process
...but unfortunately ctypes is available only in Python 2.5 (and we support 
2.4).
One solution could be to decide whether using ctypes or _psutil_posix.c at 
runtime in setup.py, depending on whether we manage to import ctypes.

= API =

My idea is to have a unique "nice" property which can be used for both getting 
and setting process niceness, as such:

>>> p = psutil.Process(3134)
>>> p.nice
0
>>> p.nice = 10
>>> p.nice
10

= Windows = 

I think there's something similar for windows as well, altough it goes under a 
different name. I haven't looked into it yet though.

Original issue reported on code.google.com by g.rodola on 25 Dec 2010 at 5:45

GoogleCodeExporter commented 9 years ago
Implemented in r855 and r856.
I added a new _psutil_posix.c module which is invoked also on Linux for now.
We'll see later whether using the ctypes variant. 

Original comment by g.rodola on 25 Dec 2010 at 9:17

GoogleCodeExporter commented 9 years ago
On Windows this is possible by using GetPriorityClass and SetPriorityClass 
functions:
http://msdn.microsoft.com/en-us/library/ms686219(v=vs.85).aspx
http://msdn.microsoft.com/en-us/library/ms683211(v=vs.85).aspx

We'll also have to expose all the *_PRIORITY_CLASS constants, presumably 
accessible as psutil.*_PRIORITY_CLASS.

Original comment by g.rodola on 25 Dec 2010 at 9:46

GoogleCodeExporter commented 9 years ago

Original comment by g.rodola on 25 Dec 2010 at 9:46

GoogleCodeExporter commented 9 years ago
Implemented on Windows in r861.
*_PRIORITY_CLASS constants are defined at psutil module level.

I wonder if it is a good idea to rename this as "priority" rather than "nice".
For Windows it would definitively make sense but on UNIX I'm not sure whether 
there's a difference between the two terms.

Maybe provide both names making Process.priority be an alias for Process.nice?

Original comment by g.rodola on 27 Dec 2010 at 12:37

GoogleCodeExporter commented 9 years ago
Python in other instances tends to side with the UNIX model and adapt Windows 
to it rather than the other way around. For example stat() in Python even 
though not all fields are available/relevant on Windows, or our use of kill() 
rather than TerminateProcess() etc. 

I suggest sticking with "nice" since it's the terminology for every other 
platform except windows and priority is the same concept anyway.

Original comment by jlo...@gmail.com on 27 Dec 2010 at 7:54

GoogleCodeExporter commented 9 years ago

Original comment by g.rodola on 20 Mar 2011 at 9:55

GoogleCodeExporter commented 9 years ago

Original comment by g.rodola on 9 Jun 2011 at 10:33

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
Updated csets after the SVN -> Mercurial migration:
r855 == revision d4a210bfdf95
r861 == revision f818d19e5160

Original comment by g.rodola on 2 Mar 2013 at 11:57