Reputation: 21
I have script like this
import os
import time
os.chdir('/sys/class/net/wlan1/statistics')
while True:
up = open('tx_bytes').read()
time.sleep(1)
print 'UP:',up
And the value is
UP: 2177453
UP: 2177539
UP: 2177789
UP: 2177990
How to find diferent value TX bytes after delay time.sleep and Befor delay time.sleep to get bandwidth Byte persecond? Byte persecond = TXbytes_after_time_sleep - TXbytes_before_time_sleep and the value can loopen no endless with while true.
Upvotes: 0
Views: 687
Reputation: 77880
What is your worry so far? YOu have a certain amount of innate overhead, since you have to return to the start of the file. We can reduce it somewhat by getting rid of the superfluous open commands:
import time
old = 0
with open('/sys/class/net/wlan1/statistics/tx_bytes') as vol:
while True:
vol.seek(0)
new = vol.read()
time.sleep(1)
print "UP:", new-old
old = new
The seek command resets the file's "bookmark"" to byte 0, the start of the file. It's faster than reopening the file, since it's merely an integer assignment to an object attribute.
Is this granularity good enough for your purposes?
Upvotes: 1
Reputation: 26951
Just like you did I guess.
It's quite rough and not very accurate but the same time spent reading the before is taken into account in reading the after so I guess it's fine.
with open('/sys/class/net/wlan1/statistics/tx_bytes') as myfile:
before = myfile.read()
time.sleep(3) # The longer this is the more accurate it will be on average.
with open('/sys/class/net/wlan1/statistics/tx_bytes') as myfile:
after = myfile.read()
print((int(after)-int(before))/3)
Upvotes: 0