python - 使用 Python 代码获取 Linux 服务信息(性能)
问题描述
我需要使用 Python 代码获取一些服务信息。我想以非常低的 CPU 消耗运行此代码。
这是简化的代码:
import subprocess, time
start_time = time.time()
command1 = "systemctl show alsa-restore.service --property=LoadState,UnitFileState,ActiveState,SubState"
command2 = "systemctl show apt-daily.service --property=LoadState,UnitFileState,ActiveState,SubState"
command3 = "systemctl show cryptdisks-early.service --property=LoadState,UnitFileState,ActiveState,SubState"
commands = command1 + "\n" + command2 + "\n" + command3
output = subprocess.check_output(commands, shell=True).decode()
end_time = time.time()
print(end_time-start_time)
实际代码有很多服务,并且这些代码运行非常频繁。什么可以使此代码运行得更快?有没有其他方法可以更快地获取这些信息?
System: Debian-like Linux x64, Python: 3.9
解决方案
推荐阅读
- ios - UICollectionViewCells 没有正确显示内容
- python - UDP 消息的发送速度比接收速度快
- python - 如何将切片对象转换为布尔索引?
- javascript - 使用 javascript 加载 HTML 文件
- python - 将列的值转换为值为 1 和 0 的列的标题(相应地)[python]
- r - 当我从 R 执行 excel 输出时,小数点在列中消失了?
- python - 使用 pyspark 包含 1 到 3 个 n-gram 的 TF-IDF
- sql-server - 减少 EF Core 字符串鉴别器列大小以允许索引
- javascript - console.log 不断返回“登录错误”而不是“登录”
- javascript - 在 js 或 jquery 中更改 YouTube 的 css 背景值?