Last active
August 29, 2020 12:13
-
-
Save akarsh1995/820bc84d208e55fec52d3db98b48ffca to your computer and use it in GitHub Desktop.
Get the process information using psutil in python. Useful to log usage in prod.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import psutil | |
| def memory_usage_psutil(pid_or_proc_name): | |
| """Find the memory usage using psutil""" | |
| pid_or_proc_name = str(pid_or_proc_name) | |
| mem_info = None | |
| # iter over processes and find the desired one | |
| for proc in psutil.process_iter(['pid', 'name', 'username']): | |
| if ( | |
| pid_or_proc_name in proc.name().lower() | |
| or pid_or_proc_name in str(proc.pid) | |
| ): | |
| mem_info = proc.memory_info().rss / 1024 / 1024 | |
| return "{:.2f} MB".format(mem_info) | |
| if __name__ == "__main__": | |
| import argh | |
| argh.dispatch_command(memory_usage_psutil) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| psutil |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment