![](/img/trans.png)
[英]How to get CPU usage in python 2.7 without using PSUtil
[英]How to monitor usage of CPU when function is called in Python psutil?
嘿,我正在學習 psutil package,我想知道當 function 正在進行時如何顯示當前的 CPU 使用率? 我想我需要一些線程或類似的東西,但是怎么做呢? 謝謝你的任何答案。
import psutil
import random
def iHateThis():
tab = []
for i in range(100000):
tab.append(random.randint(1, 10000))
tab.sort()
return tab;
while(True):
currentProcess = psutil.Process()
print(currentProcess.cpu_percent(interval=1))
您可以使用threading
來運行iHateThis
或使用cpu_percent()
運行 function。 我選擇第二個版本。 我將在線程中運行cpu_percent()
。
因為它使用while True
所以線程將永遠運行並且不會有很好的方法來停止線程所以我使用全局變量running
while running
有方法來停止這個循環。
import threading
import psutil
def display_cpu():
global running
running = True
currentProcess = psutil.Process()
# start loop
while running:
print(currentProcess.cpu_percent(interval=1))
def start():
global t
# create thread and start it
t = threading.Thread(target=display_cpu)
t.start()
def stop():
global running
global t
# use `running` to stop loop in thread so thread will end
running = False
# wait for thread's end
t.join()
現在我可以使用它來啟動和停止將顯示 CPU 的線程。 因為我可能必須使用Ctrl+C
停止進程,所以它會引發錯誤,所以我使用try/finally
來停止線程,即使會有錯誤。
def i_hate_this():
tab = []
for i in range(1000000):
tab.append(random.randint(1, 10000))
tab.sort()
return tab
# ---
start()
try:
result = i_hate_this()
finally: # stop thread even if I press Ctrl+C
stop()
完整代碼:
import random
import threading
import psutil
def display_cpu():
global running
running = True
currentProcess = psutil.Process()
# start loop
while running:
print(currentProcess.cpu_percent(interval=1))
def start():
global t
# create thread and start it
t = threading.Thread(target=display_cpu)
t.start()
def stop():
global running
global t
# use `running` to stop loop in thread so thread will end
running = False
# wait for thread's end
t.join()
# ---
def i_hate_this():
tab = []
for i in range(1000000):
tab.append(random.randint(1, 10000))
tab.sort()
return tab
# ---
start()
try:
result = i_hate_this()
finally: # stop thread even if I press Ctrl+C
stop()
順便說一句:這可以轉換為 class 繼承自 class 線程,然后它可以隱藏在 class 中running
的變量。
import psutil
import random
import threading
class DisplayCPU(threading.Thread):
def run(self):
self.running = True
currentProcess = psutil.Process()
while self.running:
print(currentProcess.cpu_percent(interval=1))
def stop(self):
self.running = False
# ----
def i_hate_this():
tab = []
for i in range(1000000):
tab.append(random.randint(1, 10000))
tab.sort()
return tab
# ---
display_cpu = DisplayCPU()
display_cpu.start()
try:
result = i_hate_this()
finally: # stop thread even when I press Ctrl+C
display_cpu.stop()
它也可以轉換為上下文管理器來運行它
with display_cpu():
i_hate_this()
但我跳過這部分。
您可以使用multiprocessing
庫來做到這一點。 multiprocessing.Process
是一個 class 代表一個線程進程,使用 function 和名稱啟動,並且可以隨時使用.start()
運行。
import multiprocessing
import psutil
import random
def iHateThis():
tab = []
for i in range(100000):
tab.append(random.randint(1, 10000))
tab.sort()
return tab;
hate = multiprocessing.Process(name='hate', target=iHateThis)
hate.start()
while(True):
currentProcess = psutil.Process()
print(currentProcess.cpu_percent(interval=1))
我認為您不需要使用 psutil Process
class ,因為我認為它旨在用於監視特定進程。 使用來自@furas 的代碼片段(接受的答案),您可以使用這樣的線程來執行此操作:
def run(self):
self.run = True
while self.run:
psutil.cpu_percent(interval=1)
在以下情況下,它與接受的答案相同:
_monitor.start()
try:
for i in range(50):
time.sleep(0.2)
finally:
_monitor.stop()
如果您不想對其進行編碼,如果它對某人有任何幫助,我將在公共回購中進行: https://github.com/GTimothee/monitor
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.