From 30ed71537a7bccacdd40891bbacb76f1a9e7ff33 Mon Sep 17 00:00:00 2001 From: Eugeni Dodonov Date: Mon, 28 Sep 2009 17:47:35 -0300 Subject: reduce number of reads --- net_monitor.py | 11 +++++++---- 1 file 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 -- cgit v1.2.1