Example #1
0
 async def __call_sync(self,
                       task,
                       outputs,
                       idx,
                       limiter,
                       max_retries=1,
                       pass_fail_job=False):
     if limiter is not None:
         async with limiter:
             for retry_idx in range(max_retries):
                 try:
                     outputs[idx] = task()
                     self.update_progress_bar()
                     return None
                 except Exception as ex:
                     if retry_idx == max_retries - 1 and not pass_fail_job:
                         with except_handler():
                             raise AssertionError(
                                 "ERROR in {}th task\n".format(idx) +
                                 format_exc(
                                     name='mlchain.workflows.parallel'))
                     elif retry_idx < max_retries - 1 or not self.verbose:
                         logger.error(
                             "PARALLEL ERROR in {0}th task and retry task, run times = {1}"
                             .format(idx, retry_idx + 1))
                     else:
                         logger.debug(
                             "PASSED PARALLEL ERROR in {}th task:".format(
                                 idx) +
                             format_exc(name='mlchain.workflows.parallel'))
     else:
         for retry_idx in range(max_retries):
             try:
                 outputs[idx] = task()
                 self.update_progress_bar()
                 return None
             except Exception as ex:
                 if retry_idx == max_retries - 1 and not pass_fail_job:
                     with except_handler():
                         raise AssertionError(
                             "ERROR in {}th task\n".format(idx) +
                             format_exc(name='mlchain.workflows.parallel'))
                 elif retry_idx < max_retries - 1 or not self.verbose:
                     logger.error(
                         "PARALLEL ERROR in {0}th task and retry task, run times = {1}"
                         .format(idx, retry_idx + 1))
                 else:
                     logger.debug(
                         "PASSED PARALLEL ERROR:",
                         format_exc(name='mlchain.workflows.parallel'))
     self.update_progress_bar()
 def __getattr__(self, name):
     if name in self._cache:
         return self._cache[name]
     else:
         if not self.__check_function(name):
             if not self.__check_attribute(name) and not name.endswith(
                     '_async'):
                 with except_handler():
                     raise AssertionError(
                         "This model has no method or attribute name = {0} or it hasnt been served. The only served is: \n\
                                         Functions: {1} \n\
                                         Attributes: {2}".format(
                             name, list(self.all_func_des.keys()),
                             list(self.all_attributes)))
             else:
                 return RemoteFunction(client=self.client,
                                       url='{0}call/{1}'.format(
                                           self.pre_url, name),
                                       name=name)()
         else:
             true_function = RemoteFunction(client=self.client,
                                            url='{0}call/{1}'.format(
                                                self.pre_url, name),
                                            name=name)
             self._cache[name] = true_function
             return true_function
    def __init__(self,
                 api_key=None,
                 api_address=None,
                 serializer='json',
                 check_status=True):
        """
        Remote model
        :client: Client to communicate, which can not be None
        :name: Name of model
        :version: Version of model
        :check_status: Check model is exist or not, and get description of model
        """

        self.client = HttpClient(api_key=api_key,
                                 api_address=api_address,
                                 serializer=serializer)

        self.pre_url = ""
        self.all_func_des = None
        self.all_func_params = None

        if check_status:
            output_description = self.client.get('{0}api/description'.format(
                self.pre_url))
            if 'error' in output_description:
                with except_handler():
                    raise AssertionError(
                        "ERROR: Model {0} is not found".format(api_address))
            else:
                # output_description = output_description['output']
                self.__doc__ = output_description['__main__']
                self.all_func_des = output_description['all_func_des']
                self.all_func_params = output_description['all_func_params']
                self.all_attributes = output_description['all_attributes']

        self._cache = weakref.WeakValueDictionary()
        self.store_ = None
    def __getattr__(self, name):
        if name in self._cache:
            return self._cache[name]
        else:
            if not self.__check_function(name):
                if not self.__check_attribute(name) and not name.endswith(
                        '_async'):
                    with except_handler():
                        raise AssertionError(
                            "This model has no method or attribute name = {0}".
                            format(name))
                else:
                    return RemoteFunction(client=self.client,
                                          url='{0}call/{1}'.format(
                                              self.pre_url, name),
                                          name=name)()
            else:
                true_function = AsyncRemoteFunction(client=self.client,
                                                    url='{0}call/{1}'.format(
                                                        self.pre_url, name),
                                                    name=name)
                self._cache[name] = true_function

                return true_function