python 系统进程_在Python中监视所有系统进程
我試圖用Python在我的操作系統(Ubuntu)中收集一些進程的統計信息(生命周期、開始時間/結束時間等)
其中一個任務:我編寫了一個函數,它獲取系統中進程的名稱和ID,但它很難看。在def get_processes():
p = subprocess.Popen(['ps', '-e'], stdout=subprocess.PIPE)
raw_string, e = p.communicate()
strings_arr = raw_string.split('\n')
strings_arr = strings_arr[1:]
processes_dict = {}
# Example of ps -e ansewer: 7 ? 00:00:00 watchdog/0
for string in strings_arr:
splted = string.split()
key, value = None, None
if len(splted) == 0: continue
for item in splted:#looking for process id
if item.isdigit():
key = int(item)
break
value = splted[-1] # looking for process name
processes_dict[key] = value
return processes_dict
有人能把它變漂亮或提出更好的解決方案嗎?
抱歉,我的書寫錯誤。英語不是我的母語。在
總結
以上是生活随笔為你收集整理的python 系统进程_在Python中监视所有系统进程的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 获取相册所有uri_URI转码
- 下一篇: 1 字节的 utf-8 序列的字节 1