我目前正在使用 psutil 在 python 中开发 CPU 和 RAM 跟踪器。我在本地进行了一些尝试,一切似乎都工作正常,但是,当我部署到 Google Cloud Run 时,跟踪器无法跟踪 CPU 的使用情况。 (RAM 的跟踪器仍在工作。)
为什么 CPU 跟踪器不工作,如何修复它?
如果有帮助,我会把我的跟踪器的代码放在下面
import psutil
import os
import time
def process_memory():
"""
Use the psutil library to access the memory statistics of the process using the ID obtained with os.getpid().
The returned value corresponds to the Resident Set Size (RSS),
which is the portion of the process's memory stored in RAM.
Returns:
int: RAM memory used by the process (in bytes).
"""
process = psutil.Process(os.getpid())
mem_info = process.memory_info()
return mem_info.rss
def Tracker(func):
def wrapper(*args, **kwargs):
#####################################
## -- Before Function Execution -- ##
#####################################
# Measure Memory
mem_before = process_memory()
# Measure CPU percentage
psutil.cpu_percent(interval=None)
time.sleep(1)
##############################
## -- Function Execution -- ##
##############################
responses, final_tracker = func(*args, **kwargs)
###########################
## -- After Execution -- ##
###########################
# Measure memory
mem_after = process_memory()
consumed_memory = (mem_after - mem_before) / (1024 ** 2) # Convert bytes to MB
# Measure CPU percentage
cpu_usage = psutil.cpu_percent(interval=None)
final_tracker = {
"Memory Consumed (MB)": consumed_memory,
"CPU Consumed (%)": cpu_usage
}
return responses, final_tracker
return wrapper
我尝试更改云运行的配置以将 CPU 设置为始终分配的状态,但这不起作用。
Procfile
:
web: python3 main.py
requirements.txt
:
google-cloud-profiler==4.1.0
psutil==6.0.0
main.py
:
import googlecloudprofiler
import psutil
import os
import time
def process_memory():
process = psutil.Process(os.getpid())
mem_info = process.memory_info()
return mem_info.rss
def Tracker(func):
def wrapper(*args, **kwargs):
#####################################
## -- Before Function Execution -- ##
#####################################
# Measure Memory
mem_before = process_memory()
# Measure CPU percentage
psutil.cpu_percent(interval=None)
time.sleep(1)
##############################
## -- Function Execution -- ##
##############################
func(*args, **kwargs)
###########################
## -- After Execution -- ##
###########################
# Measure memory
mem_after = process_memory()
consumed_memory = (mem_after - mem_before) / (1024 ** 2) # Convert bytes to MB
# Measure CPU percentage
cpu_usage = psutil.cpu_percent(interval=None)
final_tracker = {
"Memory Consumed (MB)": consumed_memory,
"CPU Consumed (%)": cpu_usage
}
return final_tracker
return wrapper
@Tracker
def add(x,y: int) -> int:
return x + y
if __name__ == "__main__":
try:
googlecloudprofiler.start(
service="psutil",
service_version="0.0.1",
verbose=3,
)
except (ValueError, NotImplementedError) as exc:
print(exc)
print(add(4,6))
当我在本地运行代码时:
{'Memory Consumed (MB)': 0.0, 'CPU Consumed (%)': 12.3}
当我在本地运行代码时,例如波德曼:
{'Memory Consumed (MB)': 0.0, 'CPU Consumed (%)': 15.5}
当我在 Cloud Run 上运行代码时没有 Cloud Profiler:
{'Memory Consumed (MB)': 0.0, 'CPU Consumed (%)': 0.0}
当我使用 Cloud Profiler 在 Cloud Run 上运行代码时:
{'Memory Consumed (MB)': 2.375, 'CPU Consumed (%)': 4.0}