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.

时机终于到了: psutil 2.0发行了 ! 在过去的4个月中,我经历了python和C扩展模块的重大重写和重组 ,此版本花费了我大量的精力和精力。 要了解发生了什么变化,您可以通过运行“ hg diff -r release-1.2.1:release-2.0.0”将旧版本1.2.1的差异进行比较,这将产生超过22,000行的输出! 在那22,000行中,我试图确定该项目自4年前以来积累的所有怪癖,并且所产生的代码库现在比以往更干净,更易于管理并且完全符合PEP-7PEP-8准则。 做出一些困难的决定是因为我引入的许多更改都不是向后兼容的,所以我担心这可能会给现有用户带来痛苦。 我还是会的,但是我确信从长远来看,这种过渡将是可以很好理解的,因为它将导致更易于管理的用户代码。 好的,足够了序言,让我们看看发生了什么变化。

API变更 ( 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
  • 函数和方法的所有get_ *前缀都消失了,例如:
    • psutil.get_boot_time()-> psutil.boot_time()
    • psutil.Process.get_cpu_percent()-> psutil.Process.cpu_percent()
  • Process方法的所有set_ *前缀都消失了,并统一在一个可用于获取和设置的单一方法中,例如:
    • psutil.Process.set_nice(值)-> psutil.Process.nice(值)
  • 过程 属性被转换为方法 ,例如:
    • psutil.Process.cmdline-> psutil.Process.cmdline()
  • 模块级别的常量 (BOOT_TIME,NUM_CPUS,TOTAL_PHYMEM)变成了函数(psutil.boot_time(),psutil.cpu_count(),psutil.virtual_memory()。total)
  • 所有旧名称仍然存在,但会引发DeprecationWarning
    • 您将必须通过“ python -Wd foo.py”显式启用警告
  • 唯一完全不兼容的更改由Process属性表示,这些属性现在是方法

RST文档 ( 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/

我从来都不满意Google Code上托管的旧文档 。 Google提供的标记语言非常有限,而且不受版本控制。 新文档更加详细,它使用reStructuredText作为标记语言,与psutil驻留在同一代码存储库中 ,并且托管在优秀的readthedocs网站上: http ://psutil.readthedocs.org/

物理CPU数量 ( Physical CPUs count)

You’re now able to distinguish between logical and physical CPUs:

现在,您可以区分逻辑CPU和物理CPU:

>>> psutil.cpu_count()  # logical
4
>>> psutil.cpu_count(logical=False)  # physical cores only
2

Full story is in issue 427.

完整的故事在第427期中

流程实例是可哈希的 ( Process instances are hashable)

Basically this means process instances can now be checked for equality and can be used with set()s:

基本上,这意味着现在可以检查流程实例是否相等,并且可以与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.

完整的故事在第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)

here. 在此处找到所有更改的列表。
OK, that’s all folks. I hope you will enjoy this new version and report your feedback.
好,那是所有人。 希望您喜欢这个新版本并报告您的反馈。

翻译自: https://www.pybloggers.com/2014/03/psutil-2-0/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值