def post(self,request): # 创建任务 # 后送到云脑 # 成功后再录入数据库 user = auth.get_user(request) form_dict=request.data print("MISSION DICT: ",form_dict) name=form_dict["name"] try: algo=models.customize_algo.objects.get(id=form_dict["algoID"]) except BaseException as e: print("DATABASE ERROR:",repr(e)) res=errParser() return Response(data=res) project_path=algo.project_path main_file=algo.start_path param,res=parse_param(form_dict) if res: return Response(data=res) index=form_dict['resource'] resource={ k:v for k,v in zip(RES_TYPE[0],RES_TYPE[index])} info=API_tools.mission_submit( job_name=name.lower(), project_dir=project_path, main_file=main_file, param=param, resource=resource, username=user.username, password=user.first_name, ) print("TYPE is: ", type(info)) print("RETURN INFO IS: ", info) '''RETURN INFO IS: { 'code': 'S000', 'msg': 'update job kktestnet2 successfully', 'payload': { 'jobId': 'fbb98fc00e9d0011eb0891304939b5259323'}}''' if type(info) != dict or info.__contains__("code") and info["code"]!="S000": res=errParser(errmessage="") else: job=info['payload'] # print("JOB INFO: ",job) res=back_untils.insert_train_job(user,job["jobId"],algo) res=errParser(errmessage=res[0]) if len(res) else Parser(res) return Response(data=res)
def AutoSearch(method:str,hyper:dict,package:dict,api_config:dict,interval:int=60): print("子进程(%s) 开始执行,父进程为(%s)" % (os.getpid(), os.getppid())) time.sleep(5) print("========MISSION REPORT========") print("pid: %s METHOD is: "%(os.getpid()),method) time.sleep(5) print("-"*24) time.sleep(5) print("pid: %s Hypers: "%(os.getpid())) a=[print(k,v) for k,v in enumerate(hyper.items())] print("-"*24) time.sleep(5) print("pid: %s Job config is: "%(os.getpid())) a=[print(k,v) for k,v in package.items()] print("-"*24) time.sleep(5) print("pid: %s Search config is: "%(os.getpid())) a=[print(k,v) for k,v in api_config.items()] print("="*24) return optimizer=None # 【需要字典形式列出的黑盒方法集合】 for ii in range(hyper['epoch']): points=optimizer.suggest(hyper['suggest']) success=[] jobid=[] for jj,point in enumerate(points): # 创建每个建议参数的任务 print(point) param=get_param(package['param'],point) # 【需要知道point的形式】 info=API_tools.mission_submit( job_name="as_"+package['job_name'], project_dir=package['project_dir'], main_file = package['main_file'], param=param, resource=package['resource'], username=package['username'], password=package['password'],) if type(info) != dict or info.__contains__("code") and info["code"]!="S000": print("ERROR: mission creation failed.\n* Epoch:{}, P:{}, {}".format(ii,jj,point)) success.append(False) else: jobid.append(info['payload']['jobId']) success.append(True) if not all(success): print("Some mission failed, exit this search procedure.") break finish=[False]*hyper['suggest'] retry=[10]*hyper['suggest'] stop=False while(all(finish) or stop): for i,jid in enumerate(jobid): info=API_tools.get_jobinfo(jid,username=package['username'],password=package['password']) if info['code'] == 'S000': status=info['payload']['jobStatus']['state'].lower() if status == 'succeeded': finish[i] = True if status == 'waiting': pass if status == 'running': pass if status == 'failed': stop=True if status == 'stopped': stop=True else: retry[i]-=1 if stop : break stop=any([x<0 for x in retry]) time.sleep(interval) if all(finish) and not stop: ylist=get_result(package['username'],hyper['result']) optimizer.observe(points,ylist)