日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

python 系统进程_在Python中监视所有系统进程

發布時間:2025/4/5 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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中监视所有系统进程的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。