0

I have this to do:

import psutil
import read_config

mem = psutil.virtual_memory()

print('Total RAM: ', (round(mem[0] / (1024 * 1024))), '(MB)')  # total

ram_usage = read_config.ram_usage

while ram_usage is True:
    mem = psutil.virtual_memory()
    print('Used RAM: ', (round(mem[3] / (1024 * 1024))), '(MB)', end="")  # used

The problem is that if I execute, the output is this:

Total RAM:  3949 (MB)
Used RAM:  3069 (MB)Used RAM:  3069 (MB)Used RAM:  3069 (MB)Used RAM:  3069 (MB)Used RAM:  3069 (MB)

How can I solve the Used RAM problem ? I want to print on the second line just value of used RAM, not to print the line everytime the condition is satisfied.

Thanks

lucians
  • 2,239
  • 5
  • 36
  • 64
  • 3
    changing `while` to `if` would certainly solve your problem wouldn't it – FlyingTeller Feb 23 '18 at 14:37
  • Are you trying to continuously monitor ram used, and update display accordingly ? – Faibbus Feb 23 '18 at 14:38
  • @FlyingTeller yes, it works. Thanks. But what if I have to use while ? – lucians Feb 23 '18 at 14:38
  • @Faibbus Yes, exactly. I would like to see just the value changing. – lucians Feb 23 '18 at 14:39
  • @Link so you do want the loop, but only print when the value has changed? – FlyingTeller Feb 23 '18 at 14:39
  • @FlyingTeller yes – lucians Feb 23 '18 at 14:40
  • [Have a look at this question](https://stackoverflow.com/questions/3173320/text-progress-bar-in-the-console). You can adopt the continous update of the same outputline from there. – Igl3 Feb 23 '18 at 14:41
  • Then you have to do something more complicated, first keep track of the previous value (to update only when the value changes), and then either make a GUI or https://stackoverflow.com/questions/6840420/python-rewrite-multiple-lines-in-the-console – Faibbus Feb 23 '18 at 14:42
  • @Faibbus that's slightly overkill for 1 line, when you can do `print('Used RAM: ', (round(mem[3] / (1024 * 1024))), '(MB) ', end="\r")`. The only issue is to print enough spaces to be sure to delete all the characters from the previous print if printed memory is smaller – Jean-François Fabre Feb 23 '18 at 14:47
  • @Jean-FrançoisFabre this is pretty close to the result. The problem is that it keep refreshing the result (RAM used line). – lucians Feb 23 '18 at 14:49
  • @Link I have added another link. Also note that you have a CPU active loop here, and also infinite (booleans are immutable, you're testing a kind of copy of your flag) – Jean-François Fabre Feb 23 '18 at 15:00
  • @Jean-FrançoisFabre thanks. It's what I am searching. From what other users said it's a little complex as task. – lucians Feb 23 '18 at 15:03

1 Answers1

1

To detect if the value has changed, simply save the old value and then print conditionally:

old_mem = 0
while ram_usage is True:
    mem = psutil.virtual_memory()
    if mem[3] != old_mem:
        old_mem = mem[3]
        print('Used RAM: ', (round(mem[3] / (1024 * 1024))), '(MB)')

This would however detect very small changes. If you just want to print when the displayed value would change, first convert to MB and then compare:

old_mem = 0
while ram_usage is True:
    mem = psutil.virtual_memory()
    mem_mb = round(mem[3] / (1024 * 1024))
    if mem_mb != old_mem:
        old_mem = mem_mb
        print('Used RAM: ', mem_mb, '(MB)')
FlyingTeller
  • 17,638
  • 3
  • 38
  • 53
  • Yes, this can be already a step ahead, but the answer is more like the above comments, for example, loading bar. – lucians Feb 23 '18 at 14:46