Question: how do i print from tqdm the process who use the highest cpu or after is hit the limit over 20% for example to to

how do i print from tqdm the process who use the highest cpu or after is hit the limit over 20% for example to to print name:google.chrome 60% cpu usage. any advice how to di it? thx import psutil from tqdm import tqdm from time import sleep def getListOfProcessSortedByMemory(): #Get list of running process sorted by Memory Usage listOfProcObjects = [] # Iterate over the list for proc in psutil.process_iter(): try: # Fetch process details as dict pinfo = proc.as_dict(attrs=['pid', 'name', 'username']) pinfo['vms'] = proc.memory_info().vms / (1024 * 1024) # Append dict to list listOfProcObjects.append(pinfo); except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess): pass # Sort list of dict by key vms i.e. memory usage listOfProcObjects = sorted(listOfProcObjects, key=lambda procObj: procObj['vms'], reverse=True) return listOfProcObjects def main(): print("*** process ID & Name Running ***") # Iterate over all running process for proc in psutil.process_iter(): try: # Get process name & pid from process object. processName = proc.name() processID = proc.pid print(processName , ' ::: ', processID) except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess): pass print('*** List of all running processes ***') listOfProcessNames = list() # Iterate over all running processes for proc in psutil.process_iter(): # Get process detail as dictionary pInfoDict = proc.as_dict(attrs=['pid', 'name', 'cpu_percent']) # Append dict of process detail in list listOfProcessNames.append(pInfoDict) # Iterate over the list of dictionary and print each elem for elem in listOfProcessNames: print(elem) print('*** Top 5 process with highest memory usage ***') listOfRunningProcess = getListOfProcessSortedByMemory() for elem in listOfRunningProcess[:5] : print(elem) if __name__ == '__main__': main() with tqdm(total=100, desc='cpu%', position=1) as cpu, tqdm(total=100, desc='ram%', position=0) as ram: while True: ram.n=psutil.virtual_memory().percent cpu.n=psutil.cpu_percent() ram.refresh() cpu.refresh() limit = 25 sleep(5) if cpu.refresh() >= limit: print (" CPU over limit ")

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!