Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Pro PowerShell For Database Developers

Authors: Bryan P Cafferky

1st Edition

1484205413, 9781484205419

More Books

Students also viewed these Databases questions