CPU & RAM Usage Monitor in Python

NeuralNine · Beginner ·💻 AI-Assisted Coding ·4y ago

Key Takeaways

This video demonstrates how to build a simple CPU and RAM usage monitoring tool in Python using the psutil library, allowing for real-time system monitoring and data visualization in the command line interface.

Full Transcript

what is going on guys welcome back in this video today we're going to build a simple command line tool that displays the cpu usage and the memory usage in real time so let's get right into it [Music] all right so for this little mini project today we're going to rely on one external python library called psutil which stands for process and system utilities and this is the library that is going to allow us to get the information on the cpu usage and on the memory usage that we're going to display here in the command line so the goal of today's video is to build a command line application that shows us the cpu usage and the memory usage in real time and preferably we want to have a visual way of representing this so not just the numbers but also something like uh not necessarily a progress bar because we're not progressing from 0 to 100 but a bar that has the range from 0 to 100 and the individual bars show how much cpu usage we have or how much memory usage we have so that is the goal for today's video the code is going to be quite similar to the code of the progress bar tutorial even though the use case is a different one it's going to be the same principle of building that bar uh visualization so if you like that video you may like this video as well um so yeah the first step is to open up a command line and to type pip install uh psutil which is the library that we're going to use as i mentioned for the data for getting the data and then we're going to import first of all time which is a core python module and we're going to import obviously psuto so uh what we want to have here is we want to have the numbers first and we want to then build a function that allows us to visualize those numbers so first of all let me show you how to get the actual usage of values this is actually quite simple and maybe this is all you care about and then you can stop watching this video but essentially you just type psutil dot cpu percent to get the cpu usage and you type psutil dot virtual memory and then dot percent here this is not a function this is a value and if we just run this code now of course you need to spell percent right but when we run this now you can see i have zero cpu usage and 47.2 memory usage at the second that i was executing this code so if i do this a couple of times maybe we will see a different value for the cpu usage but it seems like my cpu is not really working right now so it doesn't do anything but when we do this in real time so when we show this all the time you will see that this changes i mean actually we can try and do this right now we can say while true start an endless loop here and then just say time sleep i don't know one second for example and then um you can see here cpu usage 6 7.46 5.8 and so on i think just the first value is zero when it starts but then it shows you the actual cpu percentage so those are the values that we're going to use but we want to define a function that visualizes those values so what we're going to do is we're going to say def and we're going to say display underscore usage and we're going to pass to this function here first of all the cpu usage then the memory usage and then the bars the bars indicating how many characters we want to use in the command line to show this bar so if we have a hundred percent we don't have to have a hundred bars we can also have uh 10 bars one bar for every ten percent for example the default i'm going to leave it here to 50 so basically one bar for two percent points and then what we're going to do is we're going to say here the cpu percentage is going to be equal to whatever we get here as a cpu usage which is the number that we got before divided by a hundred and this hundred is going to be a float so that we have a floating point number here so this is the actual uh cpu percent that we're gonna get here now maybe cpu percent um isn't the correct word because actually we get the percent value already uh but i'm going to call it cpu percent nevertheless this is essentially just a number divided by 100 so that we have a comma value so if we have six percent cpu usage the cpu percent variable will now have 0.06 as a value so we can work with it by just multiplying um and then we're going to say now the same thing for memory usage so actually mem percent is going to be equal to memory usage divided by 100.0 and then what we're going to do is we're going to create the bar so we're going to say cpu bar this is going to be the actual visualization here and this is going to be a special character now i think that um hopefully i'm going to show you here the odd code on how to get this bar so this is a character that you have to type i'm going to copy it here for my prepared code but there is a combination where you can press the alt key and some numpad numbers to get this code here but if not you can just use any symbol so you can you can also go with a star you can also go with a point or with whatever you want essentially but i'm gonna pick this bar uh now let me see if it's alt two three maybe no it wasn't all two three all to six no all two two oh this is close okay now you can play around with that and see if you find it or hopefully if i'm not stupid i already um somehow uh showed you here the out code however you pick that character here and you multiply this character so how many times do we want to see that bar this bar is actually a filled bar so this is not empty space this is actually representing cpu or memory being used um so we're going to multiply this by the end values because we need an integer here because we want to have it x amount of times not x point something amount of times we need an integer here i'm going to say here that this is the cpu percent times the amount of bars that we have so the basic idea is that we get the percentage and we want to get as many bars as we have here so so as many bars as we have percentage so uh if we have six percent cpu usage we want to get six percent of 50 bars that's that's the idea here because of course 6 of 100 bars are six bars but if we have a different amount of bars that we want to use we want to get a different amount of bars here relatively as well so we multiply these values here uh and then we're gonna just add a bunch of empty um characters here so this dash represents not filled up space this is the space for the bar uh for the individual bars and those are the actual field bars and what we're going to do here is we're going to say this times bars minus whatever we get here so minus cpu percent times bars to get the rest of the bar or the rest of the space and um that's essentially it so this is how we do the cpu bar now essentially we can copy this here and change the name to membar and then change this here to mempercent and this here also to man percent and then we have the individual bars and all we need to do now is we need to print those which is not too trivial because we need to use an f string and we need to start by saying backslash r so that we clear um clear the row essentially and then we say cpu usage and we're going to say here we're going to use the pipe symbol here and we're going to say cpu bar is going to be so whatever string we crafted here is going to be inside of those two pipe symbols um and at the end we want to have the actual value so the actual percentage value so we're going to say cpu percent times 100 again basically reversing uh what we have here or actually now this is how i did it in my prepared code but i can actually just say cpu usage this should be fine as well right i hope i'm not um thinking in a stupid way here but then we're gonna format this to two decimal places and um add a percent sign here then i'm going to add a couple of spaces so that we don't um overwrite so that we don't go beyond the limit and then we go back because we have now two digits or three digits in a percentage value because what happens is that when you have something like seven percent and you use this back backslash r character all the time then what happens is that you have maybe 11 and then what happens is that the line is cleared but you now have again single digits and what happens is that you have something like seven percent percent so we don't want this to happen in a simple workaround is to just add two spaces here um and essentially then we say end is nothing so we don't want to have a new line here we're just going to do it like this and then we're going to say here uh mem usage here we don't do a backslash r because we don't want to clear this here and the mem usage is going to be same thing so mem bar and then mem usage 0.2 f percent space space and here we end now again with backslash r so this is how we do that and actually now all we need to do here is we need to take this and call this so we need to say while true so endless loop essentially what we're going to do is we're going to say here that we want to call display usage with psutil dot cpu percent and psutil dot uh virtual memory dot percent and we're gonna use 50 bars so actually we don't need to we're actually let's go 30 so that we fit everything into one line and we're going to say time sleep 0.5 so half a second in between the refreshes and i don't recommend running running this now in pycharm because the display doesn't work the way it should work and you're not going to run this in pycharm anyway so you're going to run this in the command line so what you can do of course is you can run a terminal in pycharm and then call it uh or you can just run cmd or the terminal directly so here i'm gonna navigate to the respective directory um and i'm gonna just say main.py and here you can see now that we have the cpu usage uh with a bar and then we also have here the mem usage with a bar and you can see how the values change now the memory usage doesn't really change because we're not opening anything here but the cpu usage is constantly changing because i'm recording because i'm moving my mouse here you can see if i move my mouse a lot this increases um so yeah this is essentially how you do that um and yeah you can now terminate this you can tweak this if you want to uh again remind me in the comments if i forget to tell you what the outco alt code here is uh now we can try here a little bit more 27 28 29 30 31 but you can see there are a bunch of symbols here that are kind of cool but yeah i don't know to be honest what the out code was you hopefully find it somewhere here or in the description down below but that is how you build a simple command line tool to show you in real time the cpu usage and the memory usage of your system so that's it for today's video i hope you enjoyed it and hope you learned something if so let me know by hitting the like button and leaving a comment in the comment section down below and of course don't forget to subscribe to this channel and hit the notification bell to not miss a single future video for free other than that thank you much for watching see you next video and bye [Music] you

Original Description

Today we build a simple CPU & RAM Usage monitoring tool for the command line in Python. ◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾ 📚 Programming Books & Merch 📚 🐍 The Python Bible Book: https://www.neuralnine.com/books/ 💻 The Algorithm Bible Book: https://www.neuralnine.com/books/ 👕 Programming Merch: https://www.neuralnine.com/shop 🌐 Social Media & Contact 🌐 📱 Website: https://www.neuralnine.com/ 📷 Instagram: https://www.instagram.com/neuralnine 🐦 Twitter: https://twitter.com/neuralnine 🤵 LinkedIn: https://www.linkedin.com/company/neuralnine/ 📁 GitHub: https://github.com/NeuralNine 🎙 Discord: https://discord.gg/JU4xr8U3dm 🎵 Outro Music From: https://www.bensound.com/
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from NeuralNine · NeuralNine · 0 of 60

← Previous Next →
1 Visualizing Stock Data With Candlestick Charts in Python
Visualizing Stock Data With Candlestick Charts in Python
NeuralNine
2 Python Beginner Tutorial #1 - Installation and First Program
Python Beginner Tutorial #1 - Installation and First Program
NeuralNine
3 Python Beginner Tutorial #2 - Variables and Data Types
Python Beginner Tutorial #2 - Variables and Data Types
NeuralNine
4 Python Beginner Tutorial #3 - Operators and User Input
Python Beginner Tutorial #3 - Operators and User Input
NeuralNine
5 Python Beginner Tutorial #4 - If Statements and Conditions
Python Beginner Tutorial #4 - If Statements and Conditions
NeuralNine
6 Python Beginner Tutorial #5 - Loops
Python Beginner Tutorial #5 - Loops
NeuralNine
7 Python Beginner Tutorial #6 - Sequences and Collections
Python Beginner Tutorial #6 - Sequences and Collections
NeuralNine
8 Python Beginner Tutorial #7 - Functions
Python Beginner Tutorial #7 - Functions
NeuralNine
9 Python Beginner Tutorial #8 - Exception Handling
Python Beginner Tutorial #8 - Exception Handling
NeuralNine
10 Python Beginner Tutorial #9 - File Operations
Python Beginner Tutorial #9 - File Operations
NeuralNine
11 Python Beginner Tutorial #10 - String Functions
Python Beginner Tutorial #10 - String Functions
NeuralNine
12 Python Intermediate Tutorial #1 - Classes and Objects
Python Intermediate Tutorial #1 - Classes and Objects
NeuralNine
13 Python Intermediate Tutorial #2 - Inheritance
Python Intermediate Tutorial #2 - Inheritance
NeuralNine
14 Python Intermediate Tutorial #3 - Multithreading
Python Intermediate Tutorial #3 - Multithreading
NeuralNine
15 Python Intermediate Tutorial #4 - Synchronizing Threads
Python Intermediate Tutorial #4 - Synchronizing Threads
NeuralNine
16 Python Intermediate Tutorial #5 - Events and Daemon Threads
Python Intermediate Tutorial #5 - Events and Daemon Threads
NeuralNine
17 Python Intermediate Tutorial #6 - Queues
Python Intermediate Tutorial #6 - Queues
NeuralNine
18 Python Intermediate Tutorial #7 - Sockets and Network Programming
Python Intermediate Tutorial #7 - Sockets and Network Programming
NeuralNine
19 Python Intermediate Tutorial #8 - Database Programming
Python Intermediate Tutorial #8 - Database Programming
NeuralNine
20 Python Intermediate Tutorial #9 - Recursion
Python Intermediate Tutorial #9 - Recursion
NeuralNine
21 Python Intermediate Tutorial #10 - XML Processing
Python Intermediate Tutorial #10 - XML Processing
NeuralNine
22 Python Intermediate Tutorial #11 - Logging
Python Intermediate Tutorial #11 - Logging
NeuralNine
23 Python Data Science Tutorial #1 - Anaconda and PyCharm Setup
Python Data Science Tutorial #1 - Anaconda and PyCharm Setup
NeuralNine
24 Python Data Science Tutorial #2 - NumPy Arrays
Python Data Science Tutorial #2 - NumPy Arrays
NeuralNine
25 Python Data Science Tutorial #3 - Numpy Functions
Python Data Science Tutorial #3 - Numpy Functions
NeuralNine
26 Python Data Science Tutorial #4 - Plotting Functions With Matplotlib
Python Data Science Tutorial #4 - Plotting Functions With Matplotlib
NeuralNine
27 Python Data Science Tutorial #5 - Subplots and Multiple Windows
Python Data Science Tutorial #5 - Subplots and Multiple Windows
NeuralNine
28 Python Data Science Tutorial #6 - Matplotlib Styling
Python Data Science Tutorial #6 - Matplotlib Styling
NeuralNine
29 Python Data Science Tutorial #7 - Bar Charts with Matplotlib
Python Data Science Tutorial #7 - Bar Charts with Matplotlib
NeuralNine
30 Python Data Science Tutorial #8 - Pie Charts with Matplotlib
Python Data Science Tutorial #8 - Pie Charts with Matplotlib
NeuralNine
31 Python Data Science Tutorial #9 - Plotting Histograms with Matplotlib
Python Data Science Tutorial #9 - Plotting Histograms with Matplotlib
NeuralNine
32 Python Data Science Tutorial #10 - Scatter Plots with Matplotlib
Python Data Science Tutorial #10 - Scatter Plots with Matplotlib
NeuralNine
33 Python Data Science Tutorial #11 - 3D Plotting with Matplotlib
Python Data Science Tutorial #11 - 3D Plotting with Matplotlib
NeuralNine
34 Python Data Science Tutorial #12 - Pandas Series
Python Data Science Tutorial #12 - Pandas Series
NeuralNine
35 Python Data Science Tutorial #13 - Pandas Data Frames
Python Data Science Tutorial #13 - Pandas Data Frames
NeuralNine
36 Python Data Science Tutorial #14 - Pandas Statistics
Python Data Science Tutorial #14 - Pandas Statistics
NeuralNine
37 Python Data Science Tutorial #15 - Pandas Sorting and Functions
Python Data Science Tutorial #15 - Pandas Sorting and Functions
NeuralNine
38 Python Data Science Tutorial #16 - Pandas Merging Data Frames
Python Data Science Tutorial #16 - Pandas Merging Data Frames
NeuralNine
39 Python Data Science Tutorial #17 - Pandas Queries
Python Data Science Tutorial #17 - Pandas Queries
NeuralNine
40 Python Machine Learning Tutorial #1 - What is Machine Learning?
Python Machine Learning Tutorial #1 - What is Machine Learning?
NeuralNine
41 Python Machine Learning Tutorial #2 - Linear Regression
Python Machine Learning Tutorial #2 - Linear Regression
NeuralNine
42 Python Machine Learning Tutorial #3 - K-Nearest Neighbors Classification
Python Machine Learning Tutorial #3 - K-Nearest Neighbors Classification
NeuralNine
43 Python Machine Learning #4 - Support Vector Machines
Python Machine Learning #4 - Support Vector Machines
NeuralNine
44 Python Machine Learning Tutorial #5 - Decision Trees and Random Forest Classification
Python Machine Learning Tutorial #5 - Decision Trees and Random Forest Classification
NeuralNine
45 Python Machine Learning Tutorial #6 - K-Means Clustering
Python Machine Learning Tutorial #6 - K-Means Clustering
NeuralNine
46 Python Machine Learning Tutorial #7 - Neural Networks
Python Machine Learning Tutorial #7 - Neural Networks
NeuralNine
47 Python Machine Learning Tutorial #8 - Handwritten Digit Recognition with Tensorflow
Python Machine Learning Tutorial #8 - Handwritten Digit Recognition with Tensorflow
NeuralNine
48 Generating Poetic Texts with Recurrent Neural Networks in Python
Generating Poetic Texts with Recurrent Neural Networks in Python
NeuralNine
49 Stock Portfolio Visualization with Matplotlib in Python
Stock Portfolio Visualization with Matplotlib in Python
NeuralNine
50 Analyzing Coronavirus with Python (COVID-19)
Analyzing Coronavirus with Python (COVID-19)
NeuralNine
51 Making Text Images Readable Again with Python and OpenCV
Making Text Images Readable Again with Python and OpenCV
NeuralNine
52 Neural Networks Simply Explained (Theory)
Neural Networks Simply Explained (Theory)
NeuralNine
53 Motion Filtering with OpenCV in Python
Motion Filtering with OpenCV in Python
NeuralNine
54 Top 5 Programming Languages To Learn in 2020
Top 5 Programming Languages To Learn in 2020
NeuralNine
55 Simple TCP Chat Room in Python
Simple TCP Chat Room in Python
NeuralNine
56 Image Classification with Neural Networks in Python
Image Classification with Neural Networks in Python
NeuralNine
57 Edge Detection with OpenCV in Python
Edge Detection with OpenCV in Python
NeuralNine
58 S&P 500 Web Scraping with Python
S&P 500 Web Scraping with Python
NeuralNine
59 Simple Sentiment Text Analysis in Python
Simple Sentiment Text Analysis in Python
NeuralNine
60 Introduction - Algorithms & Data Structures #1
Introduction - Algorithms & Data Structures #1
NeuralNine

This video teaches how to build a simple CPU and RAM usage monitoring tool in Python using psutil, allowing for real-time system monitoring and data visualization in the command line interface. The tool continuously updates the display with the current CPU and RAM usage, providing a useful system monitoring utility.

Key Takeaways
  1. Open a command line and type pip install psutil
  2. Import time and psutil modules
  3. Use psutil.cpu_percent() to get CPU usage
  4. Use psutil.virtual_memory().percent to get memory usage
  5. Define a function to display CPU and memory usage as a bar
  6. Use a while loop to continuously update CPU and memory usage
  7. Call display usage with psutil's cpu percent and virtual memory percent
💡 The psutil library provides an easy-to-use interface for retrieving system resource usage information, making it a valuable tool for building system monitoring utilities in Python.

Related Reads

📰
Beyond Replication: Theo Browne on AI's 'Skeuomorphic Phase' and the Future of Development
Learn about AI's 'Skeuomorphic Phase' and its implications on the future of development with Theo Browne's insights
Dev.to AI
📰
Rust and Artificial Intelligence: The Rust Foundation's Position
Learn about the Rust Foundation's stance on Rust and Artificial Intelligence and its potential applications
Hacker News
📰
Struggling with manual coding despite understanding basics – AI vs. Traditional Path?
Learn to leverage AI tools for coding and focus on high-level problem-solving to become a successful problem-led entrepreneur
Reddit r/learnprogramming
📰
10 Python AI Automation Scripts for Devs
Boost productivity with 10 Python AI automation scripts, including image classification and NLP tasks, to streamline dev workflows
Dev.to AI
Up next
Free WooCommerce Product Carousel Using Elementor Angie AI | Product Marquee Widget
Quick Tips - Web Desiign & Ai Tools
Watch →