Wednesday, 13 March 2024

Learn psutil library in Python 🧵:

 


Learn psutil library in Python

pip install psutil

1. Getting CPU Information:

import psutil

# Get CPU information

cpu_count = psutil.cpu_count()

cpu_percent = psutil.cpu_percent(interval=1)


print("CPU Count:", cpu_count)

print("CPU Percent:", cpu_percent)


#clcoding.com 

CPU Count: 8

CPU Percent: 6.9

2. Getting Memory Information:

import psutil

# Get memory information

memory = psutil.virtual_memory()

total_memory = memory.total

available_memory = memory.available

used_memory = memory.used

percent_memory = memory.percent

print("Total Memory:", total_memory)

print("Available Memory:", available_memory)

print("Used Memory:", used_memory)

print("Memory Percent:", percent_memory)

#clcoding.com

Total Memory: 8446738432

Available Memory: 721600512

Used Memory: 7725137920

Memory Percent: 91.5

3. Listing Running Processes:

import psutil

# List running processes

for process in psutil.process_iter():

    print(process.pid, process.name())

    #clcoding.com

0 System Idle Process

4 System

124 Registry

252 chrome.exe

408 PowerToys.Peek.UI.exe

436 msedge.exe

452 svchost.exe

504 smss.exe

520 svchost.exe

532 RuntimeBroker.exe

544 TextInputHost.exe

548 svchost.exe

680 csrss.exe

704 fontdrvhost.exe

768 wininit.exe

776 chrome.exe

804 chrome.exe

848 services.exe

924 lsass.exe

1036 WUDFHost.exe

1100 svchost.exe

1148 svchost.exe

1160 SgrmBroker.exe

1260 dllhost.exe

1284 PowerToys.exe

1328 svchost.exe

1392 svchost.exe

1400 svchost.exe

1408 svchost.exe

1488 svchost.exe

1504 svchost.exe

1512 svchost.exe

1600 SmartAudio3.exe

1608 svchost.exe

1668 svchost.exe

1716 svchost.exe

1724 IntelCpHDCPSvc.exe

1732 svchost.exe

1752 svchost.exe

1796 TiWorker.exe

1828 svchost.exe

1920 chrome.exe

1972 svchost.exe

1992 svchost.exe

2016 svchost.exe

2052 svchost.exe

2060 svchost.exe

2068 IntelCpHeciSvc.exe

2148 igfxCUIService.exe

2168 svchost.exe

2224 svchost.exe

2260 svchost.exe

2316 svchost.exe

2360 chrome.exe

2364 svchost.exe

2400 MsMpEng.exe

2420 svchost.exe

2428 svchost.exe

2448 PowerToys.FancyZones.exe

2480 screenrec.exe

2488 svchost.exe

2496 svchost.exe

2504 svchost.exe

2552 svchost.exe

2604 svchost.exe

2616 MemCompression

2716 svchost.exe

2792 chrome.exe

2796 dasHost.exe

2804 chrome.exe

2852 svchost.exe

2876 svchost.exe

2932 CxAudioSvc.exe

3016 svchost.exe

3240 svchost.exe

3416 svchost.exe

3480 svchost.exe

3536 spoolsv.exe

3620 svchost.exe

3660 svchost.exe

3700 svchost.exe

3752 RuntimeBroker.exe

3848 taskhostw.exe

3976 svchost.exe

3984 svchost.exe

3992 svchost.exe

4000 svchost.exe

4008 svchost.exe

4016 svchost.exe

4024 svchost.exe

4032 svchost.exe

4100 svchost.exe

4132 OneApp.IGCC.WinService.exe

4140 AnyDesk.exe

4148 armsvc.exe

4156 CxUtilSvc.exe

4208 WMIRegistrationService.exe

4284 msedge.exe

4312 svchost.exe

4320 AGMService.exe

4340 svchost.exe

4488 chrome.exe

4516 svchost.exe

4584 svchost.exe

4720 jhi_service.exe

4928 chrome.exe

5004 chrome.exe

5176 dwm.exe

5348 svchost.exe

5368 Flow.exe

5380 svchost.exe

5536 chrome.exe

5540 chrome.exe

5584 audiodg.exe

5620 svchost.exe

5724 svchost.exe

5776 svchost.exe

5992 ctfmon.exe

6032 CompPkgSrv.exe

6056 SearchProtocolHost.exe

6076 msedge.exe

6120 SearchIndexer.exe

6128 RuntimeBroker.exe

6156 svchost.exe

6192 MoUsoCoreWorker.exe

6380 PowerToys.PowerLauncher.exe

6424 PowerToys.Awake.exe

6480 msedge.exe

6596 svchost.exe

6740 svchost.exe

6792 winlogon.exe

6856 TrustedInstaller.exe

6872 svchost.exe

6888 igfxEM.exe

6908 svchost.exe

6948 chrome.exe

7140 csrss.exe

7296 PowerToys.KeyboardManagerEngine.exe

7336 WhatsApp.exe

7348 chrome.exe

7416 chrome.exe

7440 MusNotifyIcon.exe

7444 StartMenuExperienceHost.exe

7480 svchost.exe

7520 chrome.exe

7556 SearchApp.exe

7560 SecurityHealthService.exe

7720 msedge.exe

8220 MmReminderService.exe

8316 RuntimeBroker.exe

8636 svchost.exe

8836 python.exe

9088 ShellExperienceHost.exe

9284 svchost.exe

9344 NisSrv.exe

9560 msedge.exe

9664 chrome.exe

9736 chrome.exe

9784 SearchApp.exe

9808 svchost.exe

9868 python.exe

9884 svchost.exe

9908 chrome.exe

9936 chrome.exe

9996 QtWebEngineProcess.exe

10012 taskhostw.exe

10024 chrome.exe

10148 svchost.exe

10228 svchost.exe

10236 PowerToys.CropAndLock.exe

10304 Taskmgr.exe

10324 Video.UI.exe

10584 svchost.exe

10680 chrome.exe

10920 LockApp.exe

11064 chrome.exe

11176 chrome.exe

11188 msedge.exe

11396 msedge.exe

11500 QtWebEngineProcess.exe

11592 svchost.exe

12132 msedge.exe

12212 RuntimeBroker.exe

12360 RuntimeBroker.exe

12500 chrome.exe

12596 python.exe

12704 chrome.exe

12744 svchost.exe

12832 svchost.exe

12848 MicTray64.exe

12852 fontdrvhost.exe

12992 chrome.exe

13092 chrome.exe

13268 chrome.exe

13332 chrome.exe

13388 sihost.exe

13572 chrome.exe

13760 SecurityHealthSystray.exe

13792 msedge.exe

13880 fodhelper.exe

13900 chrome.exe

14160 UserOOBEBroker.exe

14220 RuntimeBroker.exe

14260 chrome.exe

14356 msedge.exe

14572 chrome.exe

14648 chrome.exe

14696 PowerToys.AlwaysOnTop.exe

14852 chrome.exe

14868 PowerToys.ColorPickerUI.exe

14876 conhost.exe

14888 PowerToys.PowerOCR.exe

14948 chrome.exe

15324 explorer.exe

4. Getting Process Information:

252

import psutil

# Get information for a specific process

pid = 252  # Replace with the process ID of interest

process = psutil.Process(pid)

print("Process Name:", process.name())

print("Process Status:", process.status())

print("Process CPU Percent:", process.cpu_percent(interval=1))

print("Process Memory Info:", process.memory_info())

#clcoding.com

Process Name: chrome.exe

Process Status: running

Process CPU Percent: 0.0

Process Memory Info: pmem(rss=29597696, vms=24637440, num_page_faults=14245, peak_wset=37335040, wset=29597696, peak_paged_pool=635560, paged_pool=635560, peak_nonpaged_pool=21344, nonpaged_pool=17536, pagefile=24637440, peak_pagefile=33103872, private=24637440)

5. Killing a Process:

import psutil

# Kill a process

pid_to_kill = 10088  

# Replace with the process ID to kill

process_to_kill = psutil.Process(pid_to_kill)

process_to_kill.terminate()

#clcoding.com

6. Getting Disk Usage:

import psutil

# Get disk usage information

disk_usage = psutil.disk_usage('/')

total_disk_space = disk_usage.total

used_disk_space = disk_usage.used

free_disk_space = disk_usage.free

disk_usage_percent = disk_usage.percent

print("Total Disk Space:", total_disk_space)

print("Used Disk Space:", used_disk_space)

print("Free Disk Space:", free_disk_space)

print("Disk Usage Percent:", disk_usage_percent)

#clcoding.com

Total Disk Space: 479491600384

Used Disk Space: 414899838976

Free Disk Space: 64591761408

Disk Usage Percent: 86.5

0 Comments:

Post a Comment

Popular Posts

Categories

AI (33) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (146) C (77) C# (12) C++ (82) Course (67) Coursera (198) Cybersecurity (24) data management (11) Data Science (106) Data Strucures (8) Deep Learning (13) Django (14) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Google (21) Hadoop (3) HTML&CSS (47) IBM (25) IoT (1) IS (25) Java (93) Leet Code (4) Machine Learning (46) Meta (18) MICHIGAN (5) microsoft (4) Nvidia (1) Pandas (3) PHP (20) Projects (29) Python (893) Python Coding Challenge (285) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (2) Software (17) SQL (42) UX Research (1) web application (8)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses