Monday, March 10, 2014

psutil 2.0

The time has finally come: psutil 2.0 is out! This is a release which took me a considerable amount of effort and careful thinking during the past 4 months as I went through a major rewrite and reorganization of both python and C extension modules. To get a sense of how much has changed you can compare the differences with old 1.2.1 version by running "hg diff -r release-1.2.1:release-2.0.0" which will produce more than 22,000 lines of output! In those 22k lines I tried to nail down all the quirks the project had accumulated since its start 4 years ago and the resulting code base is now cleaner than ever, more manageable and fully compliant with PEP-7 and PEP-8 guidelines.
There were some difficult decisions because many of the changes I introduced are not backward compatible so I was concerned with the pain this may cause existing users. I kind of still am, but I'm sure the transition will be well perceived on the long run as it will result in more manageable user code. OK, enough with the preface and let's see what changed.

API changes

I already wrote a detailed blog post about what changed so I recommend you to use that as the official reference on how to port your code. Long story short:
  • all get_* prefixes for functions and methods are gone, e.g.:
    • psutil.get_boot_time() -> psutil.boot_time()
    • psutil.Process.get_cpu_percent() -> psutil.Process.cpu_percent()  
  • all set_* prefixes for Process methods are gone and were unified in a single method which can be used for both getting and setting, e.g:
    • psutil.Process.set_nice(value) -> psutil.Process.nice(value)
  • Process properties were turned into methods, e.g.:
    • psutil.Process.cmdline -> psutil.Process.cmdline()   
  • module level constants (BOOT_TIME, NUM_CPUS, TOTAL_PHYMEM) were turned into functions (psutil.boot_time(), psutil.cpu_count(), psutil.virtual_memory().total)
  • all the old names are still there but will raise a DeprecationWarning
    • you will have to explicitly enable warnings via "python -Wd foo.py" though
  • the only fully incompatible change is represented by the Process properties which are now methods

RST documentation 

I've never been happy with old doc hosted on Google Code. The markup language provided by Google is pretty limited, plus it's not put under revision control. New doc is more detailed, it uses reStructuredText as the markup language, it lives in the same code repository as psutil's and it is hosted on the excellent readthedocs web site: http://psutil.readthedocs.org/

Physical CPUs count

You're now able to distinguish between logical and physical CPUs:
>>> psutil.cpu_count()  # logical
4
>>> psutil.cpu_count(logical=False)  # physical cores only
2
Full story is in issue 427.

Process instances are hashable

Basically this means process instances can now be checked for equality and can be used with set()s:
>>> p1 = psutil.Process()
>>> p2 = psutil.Process()
>>> p1 == p2
True
>>> set((p1, p2))
set([<psutil.Process(pid=8217, name='python') at 140007043550608>])
Full story is in issue 452.

Speedups 

  • #477: process cpu_percent() is about 30% faster.
  • #478: (Linux) almost all APIs are about 30% faster on Python 3.X.

Other improvements and bugfixes

List of all changes is available here.
OK, that's all folks. I hope you will enjoy this new version and report your feedback.