Sep-21-2021, 12:38 AM
Greetings fellow pythoners, I got a huge favor to ask. I have tried everyway imaginable to turn psutil.cpu_percent() results into an integer so I can use with my custom resource monitor. It always comes back as list so I cannot use it in the IF statment. Everything else works. This is the last piece
Here is the error I get:
Here is the error I get:
if cpu_percent > max_pct : TypeError: '>' not supported between instances of 'map' and 'int'here is the whole code, but again, it is just the psutil.cpu_percent() format.
from datetime import datetime
from os.path import dirname
import os.path
import csv
import psutil
import time
max_pct = 90
#sets the sketch to run for time 60 sec x 15; we can replace with chron
t_end = time.time() + 60 * 1
while time.time() < t_end:
def main():
#my attempt at a universal path
expand = (dirname(__file__))
folder = os.path.join(expand, 'monitor.csv')
# date time stamp
dt = datetime.strftime(datetime.now(), '%Y-%m-%d %H:%M:%S')
# load average for 1 minute
#cpu_load = psutil.getloadavg()
# grabs the CPU percent from psutil
cpu_percent = [psutil.cpu_percent()]
#change cpu_percent from list to int However it definatley is not working
cpu_percent = map(int,cpu_percent)
# grabs the RAM usage from psutil
mem_usage = [psutil.virtual_memory().percent]
# setup parameter output
params = [dt]
params.extend(cpu_percent)
params.extend(mem_usage)
time.sleep(2)
# sets up the line for input into the CSV
line = ','.join([str(x) for x in params])
# starts the CSV writing
with open(folder, 'a') as f:
f.write(line + '\n')
# gives some user feedback for debugging
print(line)
if cpu_percent > max_pct :
print('finally!!!!!!!!!!!!!')
time.sleep(3)
if __name__ == "__main__":
main()
