I am trying to display system information using psutil.
I am now getting following error:
Traceback (most recent call last):
File "/home/zulfi/PycharmProjects/Classes/psutilPrintInfo.py", line 31, in <module>
File "/home/zulfi/PycharmProjects/Classes/psutilPrintInfo.py", line 27, in main
File "/home/zulfi/PycharmProjects/Classes/psutilPrintInfo.py", line 16, in system_info
mem = process.get_memory_info() / float(2 ** 30) # memory in GB
AttributeError: 'Process' object has no attribute 'get_memory_info'
My code is listed below:
I have provided the comments from where I retrieved the information.
The psutil class has both class methods and instance methods. There is so much information for each method, the distinction gets lost. Some of the functions in the program need to be called directly from psutil. Specifically: psutil.cpu_percent(), psutil.disk_usage and psutil.virtual_memory. (Apparently cpu_percent is both a class method and an instance method - but see comment next.
According to the documentation the cpu_percent() method (both kinds), must be called twice - unless an interval is specified. The first time it is called, it "initializes" internal variables and always returns 0.0. So what I did was to also call psutil.cpu_percent() before calling self.Print_Numbers(). In order to get the cpu percentage above zero, I also had to bump the number of iterations through Print_Numbers() up to 10,000.
BTW, in order sort this out, I ran the following code in IDLE, the interactive python console.
The *help()* function can be used for any class, import or library.
posted 1 week ago
Please show me your whole code so that I can understand it.
Essentially I was suggesting that the "process.xxx" be replaced by "psutil.xxx" for the three function calls, and that psutil.cpu_percent be called twice -- once before calling the code to be measured and again after.
You requested the complete program. Here is what I eventually ended up with. Some comments about alterations that went beyond just adjusting the original code.
* Added more comments so that when I come back to it the comments and code will quickly remind me of how to make use of this library.
* The code has been rearranged to emphasize what needs to be done in what order.
* Code that measures the process from the code that measures system-wide information has been separated.
* Added code to show the use of both kinds of cpu_percent.
* Used the bytes2human function to format the various values rather than guessing at their magnitude.
* Added some returns. They are completely optional, but they make a great place to set a breakpoint when debugging.
* Apparently, psutil multiplies all "percentage" results by 100. This program backs that off so they can be displayed properly.
* Formatted the percentages so that useless precision is eliminated.
* The underscore variable is a Python convention for capturing and throwing away a some value that we don't care about.
* Sometimes when I ran this program, the overall cpu percentage was still zero. Other times I got a reasonable value.
posted 1 week ago
Hi Travis Risner-Good Morning,
Thanks for your program.
I have two questions:
On which OS did you run your program: windows or Unix/Linux?
I happened to run it on a Mac, which is a variation on BSD Linux. However, the psutil library is platform agnostic so it shouldn't matter. Did you have problems running it?
As to the value for i in Print_Numbers, I changed it to use the number passed in when the instance of Counting_SysInfo was instantiated at the very bottom of the module. Perhaps the integer literal of 100_000 was confusing? One of the thoughtful changes added in Python 3.6 was allowing an underscore in numeric literals as a "thousands" separator. Python has a number of enhancements - such as generators, list comprehensions, tuples, etc. - not found in other major languages that make life easier for the software developer.
posted 1 week ago
Hi my friend,
I tried using Pycharm 3.2 community version on my linux system:
I'm guessing that the program is attempting to run on a version of Python older than Python 3.6, which the code I shared requires. The most recent release available is 3.8. If an upgrade is needed, may I suggest upgrading directly to 3.8?
Also, the most recent release of PyCharm CE is 2019.3. The recent versions of PyCharm understand f-strings and the many other recent additions to Python that make life easier. The newer versions of PyCharm can assist with writing code.