Exemple #1
0
 def _call(self, *args, **kwargs):
     callback = False
     if 'callback' in kwargs:
         callback = True
     if len(args) == 1 and isinstance(args[0], Pipeline):
         fn = self._pipelined_call
         pipeline = args[0]
         if pipeline.number_of_stacked_calls == 0:
             excep = ClientError("empty pipeline")
             if callback:
                 kwargs['callback'](excep)
             else:
                 return tornado.gen.maybe_future(excep)
         arguments = (pipeline, )
     else:
         if "__multiple_replies" in kwargs:
             fn = self._simple_call_with_multiple_replies
             arguments = tuple([kwargs["__multiple_replies"]] + list(args))
         else:
             fn = self._simple_call
             arguments = args
     if callback:
         fn(*arguments, **kwargs)
     else:
         return tornado.gen.Task(fn, *arguments, **kwargs)
Exemple #2
0
    def pubsub_pop_message(self, deadline=None):
        """Pops a message for a subscribed client.

        Args:
            deadline (int): max number of seconds to wait (None => no timeout)

        Returns:
            Future with the popped message as result (or None if timeout
                or ConnectionError object in case of connection errors).

        Raises:
            ClientError: when you are not subscribed to anything
        """
        if not self.subscribed:
            raise ClientError("you must subscribe before using "
                              "pubsub_pop_message")
        reply = None
        try:
            reply = self._reply_list.pop(0)
            raise tornado.gen.Return(reply)
        except IndexError:
            pass
        yield self._condition.wait(timeout=deadline)
        try:
            reply = self._reply_list.pop(0)
        except IndexError:
            pass
        raise tornado.gen.Return(reply)
Exemple #3
0
    def pubsub_pop_message(self, deadline=None):
        """Pops a message for a subscribed client.

        Args:
            deadline (int): max number of seconds to wait (None => no timeout)

        Returns:
            Future with the popped message as result (or None if timeout
                or ConnectionError object in case of connection errors
                or ClientError object if you are not subscribed)
        """
        if not self.subscribed:
            excep = ClientError("you must subscribe before using "
                                "pubsub_pop_message")
            raise tornado.gen.Return(excep)
        reply = None
        try:
            reply = self._reply_list.pop(0)
            raise tornado.gen.Return(reply)
        except IndexError:
            pass
        if deadline is not None:
            td = timedelta(seconds=deadline)
            yield self._condition.wait(timeout=td)
        else:
            yield self._condition.wait()
        try:
            reply = self._reply_list.pop(0)
        except IndexError:
            pass
        raise tornado.gen.Return(reply)
Exemple #4
0
 def _pubsub_subscribe(self, command, *args):
     if len(args) == 0:
         raise ClientError("you must provide at least one argument")
     results = yield Client.call(self,
                                 command,
                                 *args,
                                 __multiple_replies=len(args))
     for reply in results:
         if isinstance(reply, ConnectionError) or len(reply) != 3 or \
                 reply[0].lower() != command.lower() or reply[2] == 0:
             raise tornado.gen.Return(False)
     self.subscribed = True
     raise tornado.gen.Return(True)
Exemple #5
0
    def preconnect(self, size=-1):
        """(pre)Connects some or all redis clients inside the pool.

        Args:
            size (int): number of redis clients to build and to connect
                (-1 means all clients if pool max_size > -1)

        Raises:
            ClientError: when size == -1 and pool max_size == -1
        """
        if size == -1 and self.max_size == -1:
            raise ClientError("size=-1 not allowed with pool max_size=-1")
        limit = min(size, self.max_size) if size != -1 else self.max_size
        clients = yield [self.get_connected_client() for _ in range(0, limit)]
        for client in clients:
            self.release_client(client)
Exemple #6
0
    def get_connected_client(self):
        """Gets a connected Client object.

        If max_size is reached, this method will block until a new client
        object is available.

        Returns:
            A Future object with connected Client instance as a result
                (or ClientError if there was a connection problem)
        """
        if self.__sem is not None:
            yield self.__sem.acquire()
        client = None
        newly_created, client = self._get_client_from_pool_or_make_it()
        if newly_created:
            res = yield client.connect()
            if not res:
                LOG.warning("can't connect to %s", client.title)
                raise tornado.gen.Return(
                    ClientError("can't connect to %s" % client.title))
        raise tornado.gen.Return(client)
Exemple #7
0
 def async_call(self, *args, **kwargs):
     raise ClientError("not allowed with PubSubClient object")
Exemple #8
0
 def async_call(self, *args, **kwargs):
     """Not allowed method with PubSubClient object."""
     raise ClientError("not allowed with PubSubClient object")