Example #1
0
    def add_callback(self, method):
        """
        Attaches a mehtod that will be called when the future finishes.

        :param method: A callable from an actor that will be called
            when the future completes. The only argument for that
            method must be the future itself from wich you can get the
            result though `future.:meth:`result()``. If the future has
            already completed, then the callable will be called
            immediately.

        .. note:: This functionallity only works when called from an actor,
            specifying a method from the same actor.
        """
        from_actor = get_current()
        if from_actor is not None:
            callback = (method, from_actor.channel, from_actor.url)
            with self.__condition:
                if self.__state is not FINISHED:
                    self.__callbacks.append(callback)
                    return
            # Invoke the callback directly
            # msg = TellRequest(TELL, method, [self], from_actor.url)
            msg = {TYPE: TELL, METHOD: method, PARAMS: [self],
                   TO: from_actor.url}
            from_actor.channel.send(msg)
        else:
            raise FutureError("add_callback only works when called " +
                              "from inside an actor")
Example #2
0
 def _invoke_callbacks(self):
     for callback in self.__callbacks:
         try:
             # msg = TellRequest(TELL, callback[0], [self], callback[2])
             msg = {TYPE: TELL, METHOD: callback[0], PARAMS: [self],
                    TO: callback[2]}
             callback[1].send(msg)
         except Exception, e:
             raise FutureError('exception calling callback for %r: %r'
                               % (self, e))
Example #3
0
    def send_work(self):
        '''Sends the query to the actor for it to start executing the
        work.

        It is possible to execute once again a future that has finished
        if necessary (overwriting the results), but only one execution
        at a time.
        '''
        if self.__set_running():
            # msg = FutureRequest(FUTURE, self.__method, self.__params,
            #                     self.__channel, self.__target, self.__id)
            msg = {TYPE: FUTURE, METHOD: self.__method, PARAMS: self.__params,
                   CHANNEL: self.__channel, TO: self.__target,
                   RPC_ID: self.__id}
            self.__actor_channel.send(msg)
        else:
            raise FutureError("Future already running.")