• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

Displaying system information using psuti

 
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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>
   obj_Counting_SysInfo.main()
 File "/home/zulfi/PycharmProjects/Classes/psutilPrintInfo.py", line 27, in main
   self.system_info()
 File "/home/zulfi/PycharmProjects/Classes/psutilPrintInfo.py", line 16, in system_info
   mem = process.get_memory_info()[0] / 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.

Somebody please me how to run this program.

Zulfi.
 
Greenhorn
Posts: 11
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Zulfi, a quick Google search comes up with this: https://github.com/pythonprofilers/memory_profiler/issues/114. So try mem_info instead of get_mem_info.
 
Zulfi Khan
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Thanks a lot.

I executed the following program:



Following is the  output but cpu usage is zero:

memory usage= 12099584
sdiskusage(total=982900588544, used=141398872064, free=791501631488, percent=15.2)
0.0
virtual mem=  11815522304
rss= 12099584
0.0





Can you please tell me why cpu usage is zero? How can i get some value for cpu usage?

Also for page faults, the following link says that:

https://readthedocs.org/projects/giamptest/downloads/pdf/latest/


OS(X) and windows, for linix its shared? What does it mean?



Somebody please guide me.

Zulfi.
 
Zulfi Khan
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am getting zero with cpu utilization. I have changed the code to this:




But still I am getting zero.

Somebody please guide me.

Zulfi.
 
Ranch Hand
Posts: 43
1
Python VI Editor Postgres Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Zulfi,

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.

Travis
 
Zulfi Khan
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Please show me your whole code so that I can understand it.

Zulfi.
 
Travis Risner
Ranch Hand
Posts: 43
1
Python VI Editor Postgres Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Zulfi,

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.



Sample output:



HTH,
Travis
 
Zulfi Khan
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

What is the value for i in Print_Numbers(...) ?

Zulfi.
 
Travis Risner
Ranch Hand
Posts: 43
1
Python VI Editor Postgres Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Zulfi Khan
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi my friend,

I tried using Pycharm 3.2 community version on my linux system:

I am getting following error:

File "<fstring>", line 1
   (pageFaults = )
               ^
SyntaxError: invalid syntax



Sorry I can't solve this syntax error.

Somebody please guide me.

Zulfi.
 
Travis Risner
Ranch Hand
Posts: 43
1
Python VI Editor Postgres Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
I am going to test your electrical conductivity with this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!