|
- #coding=utf-8
-
- import os
- import json
- import requests
- import threading
- from concurrent.futures import ThreadPoolExecutor
-
-
- THGY_HOST = os.environ["THGY_HOST"] if 'THGY_HOST' in os.environ else "localhost"
- THGY_PORT = os.environ["THGY_PORT"] if 'THGY_PORT' in os.environ else 8383
- THGY_BASE_URL = f"http://{THGY_HOST}:{THGY_PORT}"
-
- class THGYApiClient:
- def __init__(self, base_url=None):
- self.base_url = base_url if base_url else THGY_BASE_URL
- self.base_url = self.base_url if self.base_url.startswith('http://') else 'http://' + self.base_url
- self.executor = ThreadPoolExecutor(10)
-
- def add_training_parameters(self, uploadordownload, taskid, size):
- url=f"{self.base_url}/api/service/training_parameters/add"
- if uploadordownload == 0:
- d={"download_size":size,"taskId":taskid,"upload_size":0}
- else:
- d={"download_size":0,"taskId":taskid,"upload_size":size}
-
- try:
- r = requests.post(url, d)
- print(r.text)
- return r.text
- except Exception as e:
- print(e)
- return None
-
- def add_training_time(self, taskGroupId, taskId, round, pullTimeMs, trainTimeMs, communicationTimeMs):
- url = f"{self.base_url}/api/service/training_time/add"
- d = {"taskId": taskId, "taskGroupId": taskGroupId, "round": round, "pullTimeMs": pullTimeMs,
- "trainTimeMs": trainTimeMs, "communicationTimeMs": communicationTimeMs}
- try:
- r = requests.post(url, d)
- print(r.text)
- return r.text
- except Exception as e:
- print(e)
- return None
-
- # 'taskId=5&taskGroupId=1&round=1&recall=0.78&precision=0.78&startTime=2020-06-24%2019:49:01.12&endTime=2020-06-24%2019:49:06.12'
- def add_task_training_data(self, taskGroupId, taskId, round, recall, precision, startTime, endTime):
- url=f"{self.base_url}/api/service/task_training_data/add"
- d={"taskId":taskId, "taskGroupId":taskGroupId,
- "round":round, "recall":recall, "precision":precision,
- "startTime":startTime, "endTime":endTime}
- try:
- r = requests.post(url, d)
- print(r.text)
- return r.text
- except Exception as e:
- print(e)
- return None
-
- def get_training_parameters(self):
- url = f"{self.base_url}/api/service/training_parameters/list"
- try:
- r = requests.get(url)
- print(r.text)
- return r.text
- except Exception as e:
- print(e)
- return None
-
-
- def async_executor(self,worker,*args):
- try:
- self.executor.submit(worker,*args).add_done_callback(lambda res: print(res.result()))
- except Exception as e:
- print(e)
-
-
- if __name__ == "__main__":
- # c = THGYApiClient(f"{THGY_HOST}:{THGY_PORT}")
- # c.async_executor(c.add_training_parameters,0,1,128)
- # c.async_executor(c.get_training_parameters)
- pass
|