diff options
author | Eugeni Dodonov <eugeni@mandriva.com> | 2009-09-28 17:47:35 -0300 |
---|---|---|
committer | Eugeni Dodonov <eugeni@mandriva.com> | 2009-09-28 17:47:35 -0300 |
commit | 30ed71537a7bccacdd40891bbacb76f1a9e7ff33 (patch) | |
tree | 318bfbb34447b75f25d01906218ae61fb016e1fe | |
parent | 1a1e64e9094c7c728f76bb8009c9885a2a570ee3 (diff) | |
download | net_monitor-30ed71537a7bccacdd40891bbacb76f1a9e7ff33.tar net_monitor-30ed71537a7bccacdd40891bbacb76f1a9e7ff33.tar.gz net_monitor-30ed71537a7bccacdd40891bbacb76f1a9e7ff33.tar.bz2 net_monitor-30ed71537a7bccacdd40891bbacb76f1a9e7ff33.tar.xz net_monitor-30ed71537a7bccacdd40891bbacb76f1a9e7ff33.zip |
reduce number of reads
-rwxr-xr-x | net_monitor.py | 11 |
1 files changed, 7 insertions, 4 deletions
diff --git a/net_monitor.py b/net_monitor.py index 7c531ca..16709c6 100755 --- a/net_monitor.py +++ b/net_monitor.py @@ -60,8 +60,9 @@ def readnet(): net[dev] = vals return net -def get_traffic(iface): - net = readnet() +def get_traffic(iface, net=None): + if not net: + net = readnet() if iface in net: bytes_in = int(net[iface][0]) bytes_out = int(net[iface][8]) @@ -296,8 +297,9 @@ class Monitor: sorted_ifaces = self.ifaces.keys() sorted_ifaces.sort() + net=readnet() for iface in sorted_ifaces: - data_in, data_out = get_traffic(iface) + data_in, data_out = get_traffic(iface,net) self.ifaces[iface] = {'data_in': 0, 'data_out': 0, 'total_in': 0, @@ -324,12 +326,13 @@ class Monitor: def update(self, interval=1): """Updates traffic counters (interval is in seconds)""" + net=readnet() for iface in self.ifaces: old_data_in = self.ifaces[iface]['data_in'] old_data_out = self.ifaces[iface]['data_out'] total_in = self.ifaces[iface]['total_in'] total_out = self.ifaces[iface]['total_out'] - data_in, data_out = get_traffic(iface) + data_in, data_out = get_traffic(iface, net) # is it the first measure? if old_data_in == 0 and old_data_out == 0: old_data_in = data_in |