Python statistics

Andrew Wilkinson andrewjwilkinson at gmail.com
Sun Aug 21 16:06:54 PDT 2005


Hi,

I noticed that the Python client API is missing a function to get the
statistics from the currently active servers. Below is a patch that
add that functionality. I couldn't find documentation on the the
format returned by the Perl API, and I couldn't work out what the code
was doing, so I've just done what seemed sensible to me. The docstring
hopefully makes it clear in what format it returns the information.

Hope this is useful,
Regards,
Andrew Wilkinson

--- memcache.py 2003-08-10 23:27:08.000000000 +0100
+++ memcache_new.py     2005-08-21 23:51:34.000000000 +0100
@@ -408,7 +408,32 @@

         return val

-
+    def get_stats(self):
+        """
+        Returns statistics for this connection and every server it is
connected to.
+
+        >>> mc.get_stats()
+
+        This functions returns a pair, the first element of which is
a dictionary
+        containing the number of times each method has been called on
this connection.
+        The second element is dictionary with an entry for each
server where each element
+        contains a dictionary with every statistic returned by that server.
+        """
+        stats = {}
+        for s in self.buckets:
+            if not s or not s.connect():
+                stats[s.ip+":"+str(s.port)] = "DEAD"
+                continue
+
+            stats[s.ip+":"+str(s.port)] = {}
+
+            s.send_cmd("stats")
+            r = s.readline()
+            while r != "END":
+                stats[s.ip+":"+str(s.port)][r.split(" ")[1]] = r.split(" ")[2]
+                r = s.readline()
+        return (self.stats, stats)
+
 class _Host:
     _DEAD_RETRY = 30  # number of seconds before retrying a dead server.


More information about the memcached mailing list