示例#1
0
    def result(self):
        """
        :return: The return value of the function for this completed task.
        """
        if self._task_result is None:
            self.get_result(wait=True)

        if not self.is_success:
            return None

        if not self._is_return_value_loaded:
            return_value = Storage().get(self._task_result.result_key,
                                         storage_type='result')
            result_type = self._task_result.get('result_type',
                                                ResultType.LEGACY_PICKLE)
            if result_type == ResultType.LEGACY_PICKLE:
                return_value = cloudpickle.loads(return_value)
                if isinstance(return_value, dict):
                    # For backwards-compatibility reasons, for legacy pickles the result is
                    # wrapped in a dictionary.
                    self._return_value = return_value['result']
                else:
                    self._return_value = return_value
            elif result_type == ResultType.JSON:
                self._return_value = json.loads(return_value.decode('utf-8'))
            else:
                raise RuntimeError(
                    "Unknown result type: %s - update your tasks client")
            self._is_return_value_loaded = True

        return self._return_value
    def result(self):
        """
        Property indicating the return value of the function for this completed task.

        :rtype: json or pickled type
        :return: The return value of the function for this completed task.
        """
        if not self.is_success:
            return None

        if not self._is_return_value_loaded:
            if self._task_result.result_size_bytes > 0:
                return_value = Storage().get(self._task_result.result_key,
                                             storage_type="result")
                result_type = self._task_result.get("result_type",
                                                    ResultType.LEGACY_PICKLE)
                if result_type == ResultType.JSON:
                    self._return_value = json.loads(
                        return_value.decode("utf-8"))
                elif result_type == ResultType.LEGACY_PICKLE:
                    return_value = pickle.loads(return_value)

                    if isinstance(return_value, dict):
                        # For backwards-compatibility reasons (the old dlrun client requires it),
                        # results to be pickled have always been wrapped in a dictionary. However,
                        # all clients since version 0.10.0 ignore all the dictionary items except
                        # for 'result', and all clients have always extracted the 'result' element.
                        # In order for the service to remain compatible with older clients, we
                        # must continue to do this even though it is wasteful.
                        self._return_value = return_value["result"]
                    else:
                        # for the above reason, this code will likely never be reached.
                        self._return_value = return_value
                else:
                    raise RuntimeError(
                        "Unknown result type: %s - update your tasks client")
            else:
                self._return_value = None

            self._is_return_value_loaded = True

        return self._return_value