Esempio n. 1
0
    def version(self):
        """
        The version command returns the server's version string.

        >>> c = Client('localhost:11211')
        >>> c.version() #doctest: +ELLIPSIS
        {'...
        """
        host_version_map = {}
        @connpool.reconnect
        def per_host_version(sock, rmap):
            socksend(sock, _qnsv(M._version))
            (_, _, keylen, _, _, status, bodylen, _, _, extra) = sockresponse(sock)

            if status != R._no_error:
                raise MemcachedError("%d: %s" % (status, extra))

            version_string = struct.unpack('!%ds' % ((bodylen-keylen), ), extra)[0]
            host_key = "%s:%s" % sock.getpeername()
            rmap[host_key] = version_string

        for r in self.hash.all_nodes():
            with connpool.pooled_connection(r) as sock:
                self.threadpool.add_task(per_host_version, sock, host_version_map)
        self.threadpool.wait()

        return host_version_map
Esempio n. 2
0
 def close(self):
     """
     Close the connections to all servers.
     
     >>> c = Client('localhost:11211')
     >>> c.close()
     """
     self.quit()
     for r in self.hash.all_nodes():
         with connpool.pooled_connection(r) as sock:
             sock.close()
Esempio n. 3
0
    def _per_host_version(self, cpool, rmap):
        with connpool.pooled_connection(cpool) as sock:
            socksend(sock, _qnsv(M._version))
            (_, _, keylen, _, _, status, bodylen, _, _, extra) = sockresponse(sock)

            if status != R._no_error:
                raise MemcachedError("%d: %s" % (status, extra))

            version_string = struct.unpack('!%ds' % ((bodylen-keylen), ), extra)[0]
            host_key = tuple(sock.getpeername())
            rmap[host_key] = version_string
Esempio n. 4
0
    def quit(self):
        """
        The quit command closes the remote socket.

        >>> c = Client('localhost:11211')
        >>> c.quit()
        True
        """
        for r in self.hash.all_nodes():
            with connpool.pooled_connection(r) as sock:
                socksend(sock, _qnsv(M._quit))
                (_, _, _, _, _, status, _, _, _, extra) = sockresponse(sock)
                if status != R._no_error:
                    raise MemcachedError("%d: %s" % (status, extra))
        return True
Esempio n. 5
0
 def _per_host_stats(self, cpool, rmap):
     host_stats = {}
     with connpool.pooled_connection(cpool) as sock:
         socksend(sock, _qnsv(M._stat))
         while 1:
             (_, _, keylen, _, _, status, bodylen, _, _, extra) = sockresponse(sock)
             if status != R._no_error:
                 raise MemcachedError("%d: %s" % (status, extra))
             if keylen == 0: # last response?
                 host_key = tuple(sock.getpeername())
                 rmap[host_key] = host_stats
                 break
             else:
                 key, value = struct.unpack('!%ds%ds' % (keylen, (bodylen-keylen)), extra)
                 host_stats[key] = value
Esempio n. 6
0
    def noop(self):
        """
        The noop command Flushes outstanding getq/getkq's and can be used
        as a keep-alive.

        >>> c = Client('localhost:11211')
        >>> c.noop()
        True
        """
        for r in self.hash.all_nodes():
            with connpool.pooled_connection(r) as sock:
                socksend(sock, _qnsv(M._noop))
                (_, _, _, _, _, status, _, _, _, extra) = sockresponse(sock)
                if status != R._no_error:
                    raise MemcachedError("%d: %s" % (status, extra))
        return True
Esempio n. 7
0
    def flush_all(self, expire=0):
        """
        The flush command flushes all data the DB. Optionally this will happen
        after `expire` seconds.

        >>> c = Client('localhost:11211')
        >>> c.flush_all()
        True
        """
        for r in self.hash.all_nodes():
            with connpool.pooled_connection(r) as sock:
                socksend(sock, _f(M._flush, expire))
                (_, _, _, _, _, status, _, _, _, extra) = sockresponse(sock)
                if status != R._no_error:
                    raise MemcachedError("%d: %s" % (status, extra))
        return True
Esempio n. 8
0
 def sock4key(self, key):
     r = self.hash.get_node(key)
     with connpool.pooled_connection(r) as sock:
         yield sock