Exemplo n.º 1
0
    def __init__(self, pool, spec, timeout=None):
        """
        Initialization

        :Parameters:
         - `pool`: Pool reference
         - `spec`: Connection spec
         - `timeout`: Communication timeout

        :Types:
         - `pool`: `MemcacheConnectionPool`
         - `spec`: ``tuple``
         - `timeout`: ``float``
        """
        self.pool = _weakref.proxy(pool)
        try:
            sock = _osutil.connect(spec, timeout=timeout, cache=3600)
        except _osutil.SocketError, e:
            raise MemcacheConnectError(str(e))
Exemplo n.º 2
0
    def __call__(self, inst, *args, **kwargs):
        """
        Decorating logic

        :Parameters:
         - `inst`: Proxy instance
         - `args`: function parameters
         - `kwargs`: function parameters

        :Types:
         - `inst`: `Sharedance`
         - `args`: ``tuple``
         - `kwargs`: ``dict``

        :return: Whatever the deocorated function returns
        :rtype: any
        """
        # pylint: disable = W0221

        try:
            sock = _osutil.connect((inst.host, inst.port),
                timeout=inst.timeout, cache=3600)
            if sock is None:
                raise SharedanceConnectError(
                    "No connectable address found for %s:%s" % (
                        inst.host, inst.port
                    )
                )
            try:
                conn = \
                    _stream.GenericStream(
                        _stream.MinimalSocketStream(sock), read_exact=True
                    )
                try:
                    kwargs['_conn'] = conn
                    return self._func(inst, *args, **kwargs)
                finally:
                    sock, _ = None, conn.close()
            finally:
                if sock is not None:
                    sock.close()
        except (_osutil.SocketError, _socket.error), e:
            raise SharedanceConnectError(str(e))