psutil 4.2.0,Windows服务和Python

New psutil 4.2.0 is out. The main feature of this is the support for Windows services:

新的psutil 4.2.0已发布。 其主要功能是对Windows服务的支持:

>>> list(psutil.win_service_iter())
[<WindowsService(name='AeLookupSvc', display_name='Application Experience') at 38850096>,
 <WindowsService(name='ALG', display_name='Application Layer Gateway Service') at 38850128>,
 <WindowsService(name='APNMCP', display_name='Ask Update Service') at 38850160>,
 <WindowsService(name='AppIDSvc', display_name='Application Identity') at 38850192>,
 ...]
>>> s = psutil.win_service_get('alg')
>>> s.as_dict()
{'binpath': 'C:WindowsSystem32alg.exe',
 'description': 'Provides support for 3rd party protocol plug-ins for Internet Connection Sharing',
 'display_name': 'Application Layer Gateway Service',
 'name': 'alg',
 'pid': None,
 'start_type': 'manual',
 'status': 'stopped',
 'username': 'NT AUTHORITYLocalService'}

I did this mainly because I find pywin32 APIs too low level. Having something like this in psutil can be useful to monitor (e.g. check if a service is alive) and discover services more easily. The code diff changes are here and here’s the doc. The API for querying a service is similar to psutil.Process. You can get a reference to a service object by using its name (which is unique for every service) and then use name(), status(), etc..:

我这样做主要是因为我发现pywin32 API的级别太低。 在psutil中包含类似的内容对于监视(例如检查服务是否处于活动状态)和更轻松地发现服务很有用。 代码差异更改在这里 ,这是doc 。 用于查询服务的API与psutil.Process相似。 您可以通过使用服务对象的名称(对于每个服务都是唯一的)来获得对服务对象的引用,然后使用name(),status()等。

>>> s = psutil.win_service_get('alg')
>>> s.name()
'alg'
>>> s.status()
'stopped'

Initially I thought to expose and provide a complete set of APIs to handle all aspects of service handling including start(), stop(), restart(), install(), uninstall() and modify() but I soon realized that I would have ended up reimplementing what pywin32 already provides at the cost of overcrowding psutil API (see my reasoning here). I think psutil should really be about monitoring, not about installing stuff, especially something as critical as a Windows service.

最初,我想公开并提供一套完整的API来处理服务处理的所有方面,包括start(),stop(),restart(),install(),uninstall()和Modify(),但是我很快意识到我会最终重新实现了pywin32已经提供的功能,但以过度使用psutil API为代价(请参阅此处的我的推理)。 我认为psutil应该真正是关于监视,而不是关于安装东西,尤其是与Windows服务一样重要的东西。

关于Windows服务的注意事项 ( Considerations about Windows services)

For those of you who are not familiar with Windows, a service is something, generally an executable (.exe), which runs on system startup and keeps running in background. We can say they are the equivalent of a UNIX init script. All service are controlled by a “manager” which keeps track of their status and metadata (e.g. description, startup type) and with that you can start and stop them. It is interesting to note that since (most) services are bound to an executable (and hence a process) you can reference the process via its PID:

对于不熟悉Windows的人来说,服务就是某种东西,通常是可执行文件(.exe),它在系统启动时运行,并始终在后台运行。 我们可以说它们等同于UNIX初始化脚本。 所有服务均由“经理”控制,该经理跟踪其状态和元数据(例如,描述,启动类型),并可以使用它们启动和停止它们。 有趣的是,由于(大多数)服务都绑定到可执行文件(因此是进程),因此您可以通过其PID引用该进程:

>>> s = psutil.win_service_get('sshd')
>>> s.pid()
1865
>>> p = psutil.Process(1865)
>>> p
<psutil.Process(pid=19547, name='sshd.exe') at 140461487781328>
>>> p.exe()
'C:CygWinbinsshd'

其他改进 ( Other improvements)

psutil 4.2.0 comes with 2 other enhancements for Linux:
psutil 4.2.0随附Linux的其他2个增强功能:
  • psutil.virtual_memory() returns a new “shared” memory field. This is the same value reported by “free” cmdline utility.
  • I changed the way how /proc was parsed. Instead of reading /proc/{pid}/status line by line I used a regular expression. Here’s the speedups:* Process.ppid() is 20% faster* Process.status() is 28% faster* Process.name() is 25% faster* Process.num_threads() is 20% faster (on Python 3 only; on Python 2 it’s a bit slower – I    suppose re module received some improvements)

链接 ( Links)

翻译自: https://www.pybloggers.com/2016/05/psutil-4-2-0-windows-services-and-python/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值