CPU & RAM Usage Monitor in Python
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Visualizing Stock Data With Candlestick Charts in Python
NeuralNine
Python Beginner Tutorial #1 - Installation and First Program
NeuralNine
Python Beginner Tutorial #2 - Variables and Data Types
NeuralNine
Python Beginner Tutorial #3 - Operators and User Input
NeuralNine
Python Beginner Tutorial #4 - If Statements and Conditions
NeuralNine
Python Beginner Tutorial #5 - Loops
NeuralNine
Python Beginner Tutorial #6 - Sequences and Collections
NeuralNine
Python Beginner Tutorial #7 - Functions
NeuralNine
Python Beginner Tutorial #8 - Exception Handling
NeuralNine
Python Beginner Tutorial #9 - File Operations
NeuralNine
Python Beginner Tutorial #10 - String Functions
NeuralNine
Python Intermediate Tutorial #1 - Classes and Objects
NeuralNine
Python Intermediate Tutorial #2 - Inheritance
NeuralNine
Python Intermediate Tutorial #3 - Multithreading
NeuralNine
Python Intermediate Tutorial #4 - Synchronizing Threads
NeuralNine
Python Intermediate Tutorial #5 - Events and Daemon Threads
NeuralNine
Python Intermediate Tutorial #6 - Queues
NeuralNine
Python Intermediate Tutorial #7 - Sockets and Network Programming
NeuralNine
Python Intermediate Tutorial #8 - Database Programming
NeuralNine
Python Intermediate Tutorial #9 - Recursion
NeuralNine
Python Intermediate Tutorial #10 - XML Processing
NeuralNine
Python Intermediate Tutorial #11 - Logging
NeuralNine
Python Data Science Tutorial #1 - Anaconda and PyCharm Setup
NeuralNine
Python Data Science Tutorial #2 - NumPy Arrays
NeuralNine
Python Data Science Tutorial #3 - Numpy Functions
NeuralNine
Python Data Science Tutorial #4 - Plotting Functions With Matplotlib
NeuralNine
Python Data Science Tutorial #5 - Subplots and Multiple Windows
NeuralNine
Python Data Science Tutorial #6 - Matplotlib Styling
NeuralNine
Python Data Science Tutorial #7 - Bar Charts with Matplotlib
NeuralNine
Python Data Science Tutorial #8 - Pie Charts with Matplotlib
NeuralNine
Python Data Science Tutorial #9 - Plotting Histograms with Matplotlib
NeuralNine
Python Data Science Tutorial #10 - Scatter Plots with Matplotlib
NeuralNine
Python Data Science Tutorial #11 - 3D Plotting with Matplotlib
NeuralNine
Python Data Science Tutorial #12 - Pandas Series
NeuralNine
Python Data Science Tutorial #13 - Pandas Data Frames
NeuralNine
Python Data Science Tutorial #14 - Pandas Statistics
NeuralNine
Python Data Science Tutorial #15 - Pandas Sorting and Functions
NeuralNine
Python Data Science Tutorial #16 - Pandas Merging Data Frames
NeuralNine
Python Data Science Tutorial #17 - Pandas Queries
NeuralNine
Python Machine Learning Tutorial #1 - What is Machine Learning?
NeuralNine
Python Machine Learning Tutorial #2 - Linear Regression
NeuralNine
Python Machine Learning Tutorial #3 - K-Nearest Neighbors Classification
NeuralNine
Python Machine Learning #4 - Support Vector Machines
NeuralNine
Python Machine Learning Tutorial #5 - Decision Trees and Random Forest Classification
NeuralNine
Python Machine Learning Tutorial #6 - K-Means Clustering
NeuralNine
Python Machine Learning Tutorial #7 - Neural Networks
NeuralNine
Python Machine Learning Tutorial #8 - Handwritten Digit Recognition with Tensorflow
NeuralNine
Generating Poetic Texts with Recurrent Neural Networks in Python
NeuralNine
Stock Portfolio Visualization with Matplotlib in Python
NeuralNine
Analyzing Coronavirus with Python (COVID-19)
NeuralNine
Making Text Images Readable Again with Python and OpenCV
NeuralNine
Neural Networks Simply Explained (Theory)
NeuralNine
Motion Filtering with OpenCV in Python
NeuralNine
Top 5 Programming Languages To Learn in 2020
NeuralNine
Simple TCP Chat Room in Python
NeuralNine
Image Classification with Neural Networks in Python
NeuralNine
Edge Detection with OpenCV in Python
NeuralNine
S&P 500 Web Scraping with Python
NeuralNine
Simple Sentiment Text Analysis in Python
NeuralNine
Introduction - Algorithms & Data Structures #1
NeuralNine
More on: AI Pair Programming
View skill →Related Reads
📰
📰
📰
📰
Beyond Replication: Theo Browne on AI's 'Skeuomorphic Phase' and the Future of Development
Dev.to AI
Rust and Artificial Intelligence: The Rust Foundation's Position
Hacker News
Struggling with manual coding despite understanding basics – AI vs. Traditional Path?
Reddit r/learnprogramming
10 Python AI Automation Scripts for Devs
Dev.to AI
🎓
Tutor Explanation
DeepCamp AI