|
@@ -54,11 +54,6 @@ else:
|
|
|
if MAX_COMPUTE:
|
|
|
FROM_REMOTE = False
|
|
|
|
|
|
-# ip_port_dict = get_ip_port()
|
|
|
-# ip = 'http://127.0.0.1'
|
|
|
-# ocr_port_list = ip_port_dict.get(ip).get("ocr")
|
|
|
-# otr_port_list = ip_port_dict.get(ip).get("otr")
|
|
|
-
|
|
|
lock = multiprocessing.RLock()
|
|
|
|
|
|
# 连接redis数据库
|
|
@@ -67,38 +62,6 @@ lock = multiprocessing.RLock()
|
|
|
redis_db = None
|
|
|
|
|
|
|
|
|
-def _interface(_dict, time_out=60, retry_times=3):
|
|
|
- try:
|
|
|
- # 重试
|
|
|
- model_type = _dict.get("model_type")
|
|
|
- while retry_times:
|
|
|
- ip_port = interface_pool(model_type)
|
|
|
- if judge_error_code(ip_port):
|
|
|
- return ip_port
|
|
|
- _url = ip_port + "/" + model_type
|
|
|
- # base64_stream = base64.b64encode(pickle.dumps(_dict))
|
|
|
- r = json.loads(request_post(_url, {"data": json.dumps(_dict),
|
|
|
- "model_type": model_type}, time_out=time_out))
|
|
|
- log("get _interface return")
|
|
|
- if type(r) == list:
|
|
|
- # 接口连不上换个端口重试
|
|
|
- if retry_times <= 1:
|
|
|
- return r
|
|
|
- else:
|
|
|
- retry_times -= 1
|
|
|
- log("retry post _interface... left times " + str(retry_times) + " " + model_type)
|
|
|
- continue
|
|
|
- if judge_error_code(r):
|
|
|
- return r
|
|
|
- return r
|
|
|
- break
|
|
|
-
|
|
|
- except TimeoutError:
|
|
|
- return [-5]
|
|
|
- except requests.exceptions.ConnectionError as e:
|
|
|
- return [-2]
|
|
|
-
|
|
|
-
|
|
|
def from_office_interface(src_path, dest_path, target_format, retry_times=1, from_remote=FROM_REMOTE):
|
|
|
try:
|
|
|
# Win10跳出超时装饰器
|
|
@@ -133,6 +96,7 @@ def from_office_interface(src_path, dest_path, target_format, retry_times=1, fro
|
|
|
file_bytes = f.read()
|
|
|
base64_stream = base64.b64encode(file_bytes)
|
|
|
start_time = time.time()
|
|
|
+ log('office _url ' + str(_url))
|
|
|
r = json.loads(request_post(_url, {"src_path": src_path,
|
|
|
"dest_path": dest_path,
|
|
|
"file": base64_stream,
|
|
@@ -178,7 +142,7 @@ def from_office_interface(src_path, dest_path, target_format, retry_times=1, fro
|
|
|
return [-1]
|
|
|
|
|
|
|
|
|
-def from_ocr_interface(image_stream, is_table=False, only_rec=False, from_remote=FROM_REMOTE):
|
|
|
+def from_ocr_interface(image_stream, is_table=0, only_rec=0, from_remote=FROM_REMOTE):
|
|
|
log("into from_ocr_interface")
|
|
|
try:
|
|
|
base64_stream = base64.b64encode(image_stream)
|
|
@@ -189,13 +153,6 @@ def from_ocr_interface(image_stream, is_table=False, only_rec=False, from_remote
|
|
|
retry_times_1 = 3
|
|
|
# 重试
|
|
|
while retry_times_1:
|
|
|
- # _ip = ip_pool("ocr", _random=True)
|
|
|
- # _port = port_pool("ocr", _random=True)
|
|
|
- # if _ip == interface_ip_list[1]:
|
|
|
- # _port = ocr_port_list[0]
|
|
|
- # _ip, _port = interface_pool("ocr")
|
|
|
- # ip_port = _ip + ":" + _port
|
|
|
- # ip_port = from_schedule_interface("ocr")
|
|
|
ip_port = interface_pool_gunicorn("ocr")
|
|
|
if judge_error_code(ip_port):
|
|
|
return ip_port
|
|
@@ -205,14 +162,14 @@ def from_ocr_interface(image_stream, is_table=False, only_rec=False, from_remote
|
|
|
"only_rec": only_rec
|
|
|
},
|
|
|
time_out=60))
|
|
|
- log("get interface return")
|
|
|
+ log("get ocr interface return")
|
|
|
if type(r) == list:
|
|
|
# 接口连不上换个端口重试
|
|
|
if retry_times_1 <= 1:
|
|
|
- # if is_table:
|
|
|
- return r, r
|
|
|
- # else:
|
|
|
- # return r
|
|
|
+ if is_table:
|
|
|
+ return r, r
|
|
|
+ else:
|
|
|
+ return r
|
|
|
else:
|
|
|
retry_times_1 -= 1
|
|
|
log("retry post ocr_interface... left times " + str(retry_times_1))
|
|
@@ -226,15 +183,15 @@ def from_ocr_interface(image_stream, is_table=False, only_rec=False, from_remote
|
|
|
globals().update({"global_ocr_model": OcrModels().get_model()})
|
|
|
r = ocr(data=base64_stream, ocr_model=globals().get("global_ocr_model"), only_rec=only_rec)
|
|
|
except TimeoutError:
|
|
|
- # if is_table:
|
|
|
- return [-5], [-5]
|
|
|
- # else:
|
|
|
- # return [-5]
|
|
|
+ if is_table:
|
|
|
+ return [-5], [-5]
|
|
|
+ else:
|
|
|
+ return [-5]
|
|
|
except requests.exceptions.ConnectionError as e:
|
|
|
- # if is_table:
|
|
|
- return [-2], [-2]
|
|
|
- # else:
|
|
|
- # return [-2]
|
|
|
+ if is_table:
|
|
|
+ return [-2], [-2]
|
|
|
+ else:
|
|
|
+ return [-2]
|
|
|
|
|
|
_dict = r
|
|
|
text_list = eval(_dict.get("text"))
|
|
@@ -256,6 +213,8 @@ def from_ocr_interface(image_stream, is_table=False, only_rec=False, from_remote
|
|
|
return text
|
|
|
except Exception as e:
|
|
|
log("from_ocr_interface error!")
|
|
|
+ log(str(traceback.print_exc()))
|
|
|
+ traceback.print_exc()
|
|
|
# print("from_ocr_interface", e, global_type)
|
|
|
if is_table:
|
|
|
return [-1], [-1]
|
|
@@ -263,26 +222,6 @@ def from_ocr_interface(image_stream, is_table=False, only_rec=False, from_remote
|
|
|
return [-1]
|
|
|
|
|
|
|
|
|
-def from_gpu_interface_flask(_dict, model_type, predictor_type):
|
|
|
- log("into from_gpu_interface")
|
|
|
- start_time = time.time()
|
|
|
- try:
|
|
|
- # 调用接口
|
|
|
- _dict.update({"predictor_type": predictor_type, "model_type": model_type})
|
|
|
- if model_type == "ocr":
|
|
|
- use_zlib = True
|
|
|
- else:
|
|
|
- use_zlib = False
|
|
|
- result = _interface(_dict, time_out=30, retry_times=2, use_zlib=use_zlib)
|
|
|
- log("from_gpu_interface finish size " + str(sys.getsizeof(_dict)) + " time " + str(time.time()-start_time))
|
|
|
- return result
|
|
|
- except Exception as e:
|
|
|
- log("from_gpu_interface error!")
|
|
|
- log("from_gpu_interface failed " + str(time.time()-start_time))
|
|
|
- traceback.print_exc()
|
|
|
- return [-2]
|
|
|
-
|
|
|
-
|
|
|
def from_gpu_interface_redis(_dict, model_type, predictor_type):
|
|
|
log("into from_gpu_interface")
|
|
|
start_time = time.time()
|
|
@@ -319,114 +258,6 @@ def from_gpu_interface_redis(_dict, model_type, predictor_type):
|
|
|
return [-2]
|
|
|
|
|
|
|
|
|
-# def from_gpu_flask_sm(_dict, model_type, predictor_type):
|
|
|
-# log("into from_gpu_share_memory")
|
|
|
-# start_time = time.time()
|
|
|
-# shm = None
|
|
|
-# try:
|
|
|
-# # 放入共享内存
|
|
|
-# _time = time.time()
|
|
|
-# np_data = _dict.get("inputs")
|
|
|
-# shm = to_share_memory(np_data)
|
|
|
-# log("data into share memory " + str(shm.name) + " " + str(time.time()-_time))
|
|
|
-#
|
|
|
-# # 调用接口
|
|
|
-# _time = time.time()
|
|
|
-# _dict.pop("inputs")
|
|
|
-# _dict.update({"predictor_type": predictor_type, "model_type": model_type,
|
|
|
-# "sm_name": shm.name, "sm_shape": np_data.shape,
|
|
|
-# "sm_dtype": str(np_data.dtype)})
|
|
|
-# result = _interface(_dict, time_out=30, retry_times=2)
|
|
|
-# log("_interface cost " + str(time.time()-_time))
|
|
|
-#
|
|
|
-# # 读取共享内存
|
|
|
-# _time = time.time()
|
|
|
-# sm_name = result.get("sm_name")
|
|
|
-# sm_shape = result.get("sm_shape")
|
|
|
-# sm_dtype = result.get("sm_dtype")
|
|
|
-# sm_dtype = get_np_type(sm_dtype)
|
|
|
-# if sm_name:
|
|
|
-# outputs = from_share_memory(sm_name, sm_shape, sm_dtype)
|
|
|
-# else:
|
|
|
-# log("from_share_memory failed!")
|
|
|
-# raise Exception
|
|
|
-# log("data from share memory " + sm_name + " " + str(time.time()-_time))
|
|
|
-#
|
|
|
-# log("from_gpu_interface finish - size " + str(sys.getsizeof(_dict)) + " - time " + str(time.time()-start_time))
|
|
|
-# return {"preds": outputs, "gpu_time": result.get("gpu_time")}
|
|
|
-# except Exception as e:
|
|
|
-# log("from_gpu_interface failed " + str(time.time()-start_time))
|
|
|
-# traceback.print_exc()
|
|
|
-# return [-2]
|
|
|
-# finally:
|
|
|
-# # del b # Unnecessary; merely emphasizing the array is no longer used
|
|
|
-# if shm:
|
|
|
-# try:
|
|
|
-# shm.close()
|
|
|
-# shm.unlink()
|
|
|
-# except FileNotFoundError:
|
|
|
-# log("share memory " + shm.name + " not exists!")
|
|
|
-# except Exception:
|
|
|
-# traceback.print_exc()
|
|
|
-#
|
|
|
-#
|
|
|
-# def from_gpu_share_memory(_dict, model_type, predictor_type):
|
|
|
-# log("into from_gpu_share_memory")
|
|
|
-# start_time = time.time()
|
|
|
-# try:
|
|
|
-# _dict.update({"model_type": model_type, "predictor_type": predictor_type})
|
|
|
-# outputs, gpu_time = share_memory_pool(_dict)
|
|
|
-# log("from_gpu_share_memory finish - size " + str(sys.getsizeof(_dict)) + " - time " + str(time.time()-start_time))
|
|
|
-# return {"preds": outputs, "gpu_time": float(gpu_time)}
|
|
|
-# except Exception as e:
|
|
|
-# log("from_gpu_interface failed " + str(time.time()-start_time))
|
|
|
-# traceback.print_exc()
|
|
|
-# return [-2]
|
|
|
-
|
|
|
-
|
|
|
-def from_otr_interface2(image_stream):
|
|
|
- log("into from_otr_interface")
|
|
|
- try:
|
|
|
- base64_stream = base64.b64encode(image_stream)
|
|
|
-
|
|
|
- # 调用接口
|
|
|
- try:
|
|
|
- if globals().get("global_otr_model") is None:
|
|
|
- globals().update({"global_otr_model": OtrModels().get_model()})
|
|
|
- print("=========== init otr model ===========")
|
|
|
- r = otr(data=base64_stream, otr_model=globals().get("global_otr_model"))
|
|
|
- except TimeoutError:
|
|
|
- return [-5], [-5], [-5], [-5], [-5]
|
|
|
- except requests.exceptions.ConnectionError as e:
|
|
|
- log("from_otr_interface")
|
|
|
- print("from_otr_interface", traceback.print_exc())
|
|
|
- return [-2], [-2], [-2], [-2], [-2]
|
|
|
-
|
|
|
- # 处理结果
|
|
|
- _dict = r
|
|
|
- points = eval(_dict.get("points"))
|
|
|
- split_lines = eval(_dict.get("split_lines"))
|
|
|
- bboxes = eval(_dict.get("bboxes"))
|
|
|
- outline_points = eval(_dict.get("outline_points"))
|
|
|
- lines = eval(_dict.get("lines"))
|
|
|
- # print("from_otr_interface len(bboxes)", len(bboxes))
|
|
|
- if points is None:
|
|
|
- points = []
|
|
|
- if split_lines is None:
|
|
|
- split_lines = []
|
|
|
- if bboxes is None:
|
|
|
- bboxes = []
|
|
|
- if outline_points is None:
|
|
|
- outline_points = []
|
|
|
- if lines is None:
|
|
|
- lines = []
|
|
|
- return points, split_lines, bboxes, outline_points, lines
|
|
|
- except Exception as e:
|
|
|
- log("from_otr_interface error!")
|
|
|
- print("from_otr_interface", traceback.print_exc())
|
|
|
- return [-1], [-1], [-1], [-1], [-1]
|
|
|
-
|
|
|
-
|
|
|
def from_otr_interface(image_stream, is_from_pdf=False, from_remote=FROM_REMOTE):
|
|
|
log("into from_otr_interface")
|
|
|
try:
|
|
@@ -723,81 +554,13 @@ def from_yolo_interface(image_stream, from_remote=FROM_REMOTE):
|
|
|
return [-11]
|
|
|
|
|
|
|
|
|
-# def from_schedule_interface(interface_type):
|
|
|
-# try:
|
|
|
-# _ip = "http://" + get_intranet_ip()
|
|
|
-# _port = ip_port_dict.get(_ip).get("schedule")[0]
|
|
|
-# _url = _ip + ":" + _port + "/schedule"
|
|
|
-# data = {"interface_type": interface_type}
|
|
|
-# result = json.loads(request_post(_url, data, time_out=10)).get("data")
|
|
|
-# if judge_error_code(result):
|
|
|
-# return result
|
|
|
-# _ip, _port = result
|
|
|
-# log("from_schedule_interface " + _ip + " " + _port)
|
|
|
-# return _ip + ":" + _port
|
|
|
-# except requests.exceptions.ConnectionError as e:
|
|
|
-# log("from_schedule_interface ConnectionError")
|
|
|
-# return [-2]
|
|
|
-# except:
|
|
|
-# log("from_schedule_interface error!")
|
|
|
-# traceback.print_exc()
|
|
|
-# return [-1]
|
|
|
-
|
|
|
-
|
|
|
-def interface_pool(interface_type, use_gunicorn=True):
|
|
|
- ip_port_flag = _global.get("ip_port_flag")
|
|
|
- ip_port_dict = _global.get("ip_port")
|
|
|
- try:
|
|
|
- if use_gunicorn:
|
|
|
- _ip = "http://127.0.0.1"
|
|
|
- _port = ip_port_dict.get(_ip).get(interface_type)[0]
|
|
|
- ip_port = _ip + ":" + str(_port)
|
|
|
- log(ip_port)
|
|
|
- return ip_port
|
|
|
-
|
|
|
- # 负载均衡, 选取ip
|
|
|
- interface_load_list = []
|
|
|
- for _ip in ip_port_flag.keys():
|
|
|
- if ip_port_dict.get(_ip).get(interface_type):
|
|
|
- load_scale = ip_port_flag.get(_ip).get(interface_type) / len(ip_port_dict.get(_ip).get(interface_type))
|
|
|
- interface_load_list.append([_ip, load_scale])
|
|
|
-
|
|
|
- if not interface_load_list:
|
|
|
- raise NotFound
|
|
|
- interface_load_list.sort(key=lambda x: x[-1])
|
|
|
- _ip = interface_load_list[0][0]
|
|
|
-
|
|
|
- # 负载均衡, 选取port
|
|
|
- ip_type_cnt = ip_port_flag.get(_ip).get(interface_type)
|
|
|
- ip_type_total = len(ip_port_dict.get(_ip).get(interface_type))
|
|
|
- if ip_type_cnt == 0:
|
|
|
- ip_type_cnt = random.randint(0, ip_type_total-1)
|
|
|
- port_index = ip_type_cnt % ip_type_total
|
|
|
- _port = ip_port_dict.get(_ip).get(interface_type)[port_index]
|
|
|
-
|
|
|
- # 更新flag
|
|
|
- current_flag = ip_type_cnt
|
|
|
- if current_flag >= 10000:
|
|
|
- ip_port_flag[_ip][interface_type] = 0
|
|
|
- else:
|
|
|
- ip_port_flag[_ip][interface_type] = current_flag + 1
|
|
|
- _global.update({"ip_port_flag": ip_port_flag})
|
|
|
- # log(str(_global.get("ip_port_flag")))
|
|
|
-
|
|
|
- ip_port = _ip + ":" + str(_port)
|
|
|
- log(ip_port)
|
|
|
- return ip_port
|
|
|
- except NotFound:
|
|
|
- log("cannot read ip from config! checkout config")
|
|
|
- return [-2]
|
|
|
- except:
|
|
|
- traceback.print_exc()
|
|
|
- return [-1]
|
|
|
-
|
|
|
-
|
|
|
def interface_pool_gunicorn(interface_type):
|
|
|
+ # if get_platform() == 'Windows':
|
|
|
+ # set_flask_global()
|
|
|
+
|
|
|
ip_port_flag_dict = _global.get("ip_port_flag")
|
|
|
ip_port_dict = _global.get("ip_port")
|
|
|
+
|
|
|
try:
|
|
|
if ip_port_dict is None or ip_port_flag_dict is None:
|
|
|
print('_global', _global.get_dict())
|
|
@@ -810,19 +573,34 @@ def interface_pool_gunicorn(interface_type):
|
|
|
port_list = []
|
|
|
for key in ip_port_flag_dict.keys():
|
|
|
temp_port_list = get_args_from_config(ip_port_dict, key, interface_type)
|
|
|
+ # print('temp_port_list', temp_port_list)
|
|
|
if not temp_port_list:
|
|
|
continue
|
|
|
+
|
|
|
+ # 该ip下的该接口总数量(可能有多gpu接口)
|
|
|
+ _port_list, _port_num_list, _ = temp_port_list[0]
|
|
|
+ # print('_port_num_list', _port_num_list)
|
|
|
+ total_port_num = sum(_port_num_list)
|
|
|
+ if total_port_num == 0:
|
|
|
+ continue
|
|
|
+
|
|
|
interface_cnt = ip_port_flag_dict.get(key).get(interface_type)
|
|
|
- if interface_cnt is not None and interface_cnt / len(temp_port_list[0]) < min_cnt:
|
|
|
+ if interface_cnt is not None and interface_cnt / total_port_num < min_cnt:
|
|
|
_ip = key
|
|
|
min_cnt = interface_cnt / len(temp_port_list[0])
|
|
|
- port_list = temp_port_list[0]
|
|
|
+
|
|
|
+ # 选定ip,设置gpu的接口候选比例
|
|
|
+ gpu_port_list = []
|
|
|
+ for k in range(len(_port_list)):
|
|
|
+ gpu_port_list += [_port_list[k]] * _port_num_list[k]
|
|
|
+ port_list = gpu_port_list
|
|
|
+ # port_list = temp_port_list[0]
|
|
|
|
|
|
# 选取端口
|
|
|
if interface_type == "office":
|
|
|
if len(port_list) == 0:
|
|
|
raise ConnectionError
|
|
|
-
|
|
|
+ port_list = [str(port_list[k] + k) for k in range(len(port_list))]
|
|
|
# 刚开始随机,后续求余
|
|
|
if min_cnt == 0:
|
|
|
_port = port_list[random.randint(0, len(port_list)-1)]
|
|
@@ -830,8 +608,8 @@ def interface_pool_gunicorn(interface_type):
|
|
|
else:
|
|
|
_port = port_list[interface_cnt % len(port_list)]
|
|
|
else:
|
|
|
- # 使用gunicorn则直接选第一个
|
|
|
- _port = port_list[0]
|
|
|
+ # 使用gunicorn则随机选
|
|
|
+ _port = random.choice(port_list)
|
|
|
|
|
|
# 更新flag
|
|
|
if ip_port_flag_dict.get(_ip).get(interface_type) >= 10000:
|
|
@@ -855,243 +633,6 @@ def interface_pool_gunicorn(interface_type):
|
|
|
return [-1]
|
|
|
|
|
|
|
|
|
-def interface_pool_gunicorn_old(interface_type):
|
|
|
- ip_flag_list = _global.get("ip_flag")
|
|
|
- ip_port_flag_dict = _global.get("ip_port_flag")
|
|
|
- ip_port_dict = _global.get("ip_port")
|
|
|
- try:
|
|
|
- if ip_flag_list is None or ip_port_dict is None or ip_port_flag_dict is None:
|
|
|
- raise NotFound
|
|
|
-
|
|
|
- if interface_type == "office":
|
|
|
- # _ip = "http://127.0.0.1"
|
|
|
- _ip = get_using_ip()
|
|
|
- # 选取端口
|
|
|
- port_list = ip_port_dict.get(_ip).get("MASTER").get(interface_type)
|
|
|
- ip_type_cnt = ip_port_flag_dict.get(_ip).get(interface_type)
|
|
|
- if ip_type_cnt == 0:
|
|
|
- _port = port_list[random.randint(0, len(port_list)-1)]
|
|
|
- else:
|
|
|
- _port = port_list[ip_type_cnt % len(port_list)]
|
|
|
- # 更新flag
|
|
|
- if ip_port_flag_dict.get(_ip).get(interface_type) >= 10000:
|
|
|
- ip_port_flag_dict[_ip][interface_type] = 0
|
|
|
- else:
|
|
|
- ip_port_flag_dict[_ip][interface_type] += 1
|
|
|
- _global.update({"ip_port_flag": ip_port_flag_dict})
|
|
|
-
|
|
|
- else:
|
|
|
- # 负载均衡, 选取ip
|
|
|
- ip_flag_list.sort(key=lambda x: x[1])
|
|
|
- if ip_flag_list[-1][1] == 0:
|
|
|
- ip_index = random.randint(0, len(ip_flag_list)-1)
|
|
|
- else:
|
|
|
- ip_index = 0
|
|
|
- _ip = ip_flag_list[ip_index][0]
|
|
|
- if "master" in _ip:
|
|
|
- port_index = 1
|
|
|
- else:
|
|
|
- port_index = 0
|
|
|
- _ip = _ip.split("_")[0]
|
|
|
- # 选取端口, 使用gunicorn则直接选第一个
|
|
|
- # _port = ip_port_dict.get(_ip).get("MASTER").get(interface_type)[0]
|
|
|
- log("_ip " + _ip)
|
|
|
- log("interface_type " + interface_type)
|
|
|
- port_list = get_args_from_config(ip_port_dict, _ip, interface_type)
|
|
|
- log("port_list" + str(port_list))
|
|
|
- if port_index >= len(port_list):
|
|
|
- port_index = 0
|
|
|
- _port = port_list[port_index][0]
|
|
|
-
|
|
|
- # # 选取端口, 使用gunicorn则直接选第一个
|
|
|
- # _ip = _ip.split("_")[0]
|
|
|
- # port_list = get_args_from_config(ip_port_dict, _ip, interface_type)
|
|
|
- # if
|
|
|
- # print(port_list)
|
|
|
- # _port = port_list[0][0]
|
|
|
-
|
|
|
- # 更新flag
|
|
|
- if ip_flag_list[ip_index][1] >= 10000:
|
|
|
- ip_flag_list[ip_index][1] = 0
|
|
|
- else:
|
|
|
- ip_flag_list[ip_index][1] += + 1
|
|
|
- _global.update({"ip_flag": ip_flag_list})
|
|
|
-
|
|
|
- ip_port = _ip + ":" + str(_port)
|
|
|
- log(ip_port)
|
|
|
- return ip_port
|
|
|
- except NotFound:
|
|
|
- log("ip_flag or ip_port_dict is None! checkout config")
|
|
|
- return [-2]
|
|
|
- except:
|
|
|
- traceback.print_exc()
|
|
|
- return [-1]
|
|
|
-
|
|
|
-
|
|
|
-# def share_memory_pool(args_dict):
|
|
|
-# np_data = args_dict.get("inputs")
|
|
|
-# _type = args_dict.get("model_type")
|
|
|
-# args_dict.update({"sm_shape": np_data.shape, "sm_dtype": str(np_data.dtype)})
|
|
|
-#
|
|
|
-# if _type == 'ocr':
|
|
|
-# port_list = ocr_port_list
|
|
|
-# elif _type == 'otr':
|
|
|
-# port_list = otr_port_list
|
|
|
-# else:
|
|
|
-# log("type error! only support ocr otr")
|
|
|
-# raise Exception
|
|
|
-#
|
|
|
-# # 循环判断是否有空的share memory
|
|
|
-# empty_sm_list = None
|
|
|
-# sm_list_name = ""
|
|
|
-# while empty_sm_list is None:
|
|
|
-# for p in port_list:
|
|
|
-# sm_list_name = "sml_"+_type+"_"+str(p)
|
|
|
-# sm_list = get_share_memory_list(sm_list_name)
|
|
|
-# if sm_list[0] == "0":
|
|
|
-# lock.acquire(timeout=0.1)
|
|
|
-# if sm_list[0] == "0":
|
|
|
-# sm_list[0] = "1"
|
|
|
-# sm_list[-1] = "0"
|
|
|
-# empty_sm_list = sm_list
|
|
|
-# break
|
|
|
-# else:
|
|
|
-# continue
|
|
|
-# lock.release()
|
|
|
-#
|
|
|
-# log(str(os.getppid()) + " empty_sm_list " + sm_list_name)
|
|
|
-#
|
|
|
-# # numpy放入共享内存
|
|
|
-# _time = time.time()
|
|
|
-# release_share_memory(get_share_memory("psm_" + str(os.getpid())))
|
|
|
-# shm = to_share_memory(np_data)
|
|
|
-# log("data into share memory " + str(shm.name) + " " + str(time.time()-_time))
|
|
|
-#
|
|
|
-# # 参数放入共享内存列表
|
|
|
-# empty_sm_list[1] = args_dict.get("md5")
|
|
|
-# empty_sm_list[2] = args_dict.get("model_type")
|
|
|
-# empty_sm_list[3] = args_dict.get("predictor_type")
|
|
|
-# empty_sm_list[4] = args_dict.get("args")
|
|
|
-# empty_sm_list[5] = str(shm.name)
|
|
|
-# empty_sm_list[6] = str(args_dict.get("sm_shape"))
|
|
|
-# empty_sm_list[7] = args_dict.get("sm_dtype")
|
|
|
-# empty_sm_list[-1] = "1"
|
|
|
-# # log("empty_sm_list[7] " + empty_sm_list[7])
|
|
|
-# close_share_memory_list(empty_sm_list)
|
|
|
-#
|
|
|
-# # 循环判断是否完成
|
|
|
-# finish_sm_list = get_share_memory_list(sm_list_name)
|
|
|
-# while True:
|
|
|
-# if finish_sm_list[-1] == "0":
|
|
|
-# break
|
|
|
-#
|
|
|
-# # 读取共享内存
|
|
|
-# _time = time.time()
|
|
|
-# sm_name = finish_sm_list[5]
|
|
|
-# sm_shape = finish_sm_list[6]
|
|
|
-# sm_shape = eval(sm_shape)
|
|
|
-# sm_dtype = finish_sm_list[7]
|
|
|
-# gpu_time = finish_sm_list[8]
|
|
|
-# sm_dtype = get_np_type(sm_dtype)
|
|
|
-# outputs = from_share_memory(sm_name, sm_shape, sm_dtype)
|
|
|
-# log(args_dict.get("model_type") + " " + args_dict.get("predictor_type") + " outputs " + str(outputs.shape))
|
|
|
-# log("data from share memory " + sm_name + " " + str(time.time()-_time))
|
|
|
-#
|
|
|
-# # 释放
|
|
|
-# release_share_memory(get_share_memory(sm_name))
|
|
|
-#
|
|
|
-# # 重置share memory list
|
|
|
-# finish_sm_list[-1] = "0"
|
|
|
-# finish_sm_list[0] = "0"
|
|
|
-#
|
|
|
-# close_share_memory_list(finish_sm_list)
|
|
|
-# return outputs, gpu_time
|
|
|
-
|
|
|
-
|
|
|
-# def interface_pool(interface_type):
|
|
|
-# try:
|
|
|
-# ip_port_dict = _global.get("ip_port")
|
|
|
-# ip_list = list(ip_port_dict.keys())
|
|
|
-# _ip = random.choice(ip_list)
|
|
|
-# if interface_type != 'office':
|
|
|
-# _port = ip_port_dict.get(_ip).get(interface_type)[0]
|
|
|
-# else:
|
|
|
-# _port = random.choice(ip_port_dict.get(_ip).get(interface_type))
|
|
|
-# log(_ip + ":" + _port)
|
|
|
-# return _ip + ":" + _port
|
|
|
-# except Exception as e:
|
|
|
-# traceback.print_exc()
|
|
|
-# return [-1]
|
|
|
-
|
|
|
-
|
|
|
-# def ip_pool(interface_type, _random=False):
|
|
|
-# ip_flag_name = interface_type + '_ip_flag'
|
|
|
-# ip_flag = globals().get(ip_flag_name)
|
|
|
-# if ip_flag is None:
|
|
|
-# if _random:
|
|
|
-# _r = random.randint(0, len(interface_ip_list)-1)
|
|
|
-# ip_flag = _r
|
|
|
-# globals().update({ip_flag_name: ip_flag})
|
|
|
-# ip_index = _r
|
|
|
-# else:
|
|
|
-# ip_flag = 0
|
|
|
-# globals().update({ip_flag_name: ip_flag})
|
|
|
-# ip_index = 0
|
|
|
-# else:
|
|
|
-# ip_index = ip_flag % len(interface_ip_list)
|
|
|
-# ip_flag += 1
|
|
|
-#
|
|
|
-# if ip_flag >= 10000:
|
|
|
-# ip_flag = 0
|
|
|
-# globals().update({ip_flag_name: ip_flag})
|
|
|
-#
|
|
|
-# log("ip_pool " + interface_type + " " + str(ip_flag) + " " + str(interface_ip_list[ip_index]))
|
|
|
-# return interface_ip_list[ip_index]
|
|
|
-#
|
|
|
-#
|
|
|
-# def port_pool(interface_type, _random=False):
|
|
|
-# port_flag_name = interface_type + '_port_flag'
|
|
|
-#
|
|
|
-# port_flag = globals().get(port_flag_name)
|
|
|
-# if port_flag is None:
|
|
|
-# if _random:
|
|
|
-# if interface_type == "ocr":
|
|
|
-# _r = random.randint(0, len(ocr_port_list)-1)
|
|
|
-# elif interface_type == "otr":
|
|
|
-# _r = random.randint(0, len(otr_port_list)-1)
|
|
|
-# else:
|
|
|
-# _r = random.randint(0, len(soffice_port_list)-1)
|
|
|
-# port_flag = _r
|
|
|
-# globals().update({port_flag_name: port_flag})
|
|
|
-# port_index = _r
|
|
|
-# else:
|
|
|
-# port_flag = 0
|
|
|
-# globals().update({port_flag_name: port_flag})
|
|
|
-# port_index = 0
|
|
|
-# else:
|
|
|
-# if interface_type == "ocr":
|
|
|
-# port_index = port_flag % len(ocr_port_list)
|
|
|
-# elif interface_type == "otr":
|
|
|
-# port_index = port_flag % len(otr_port_list)
|
|
|
-# else:
|
|
|
-# port_index = port_flag % len(soffice_port_list)
|
|
|
-# port_flag += 1
|
|
|
-#
|
|
|
-# if port_flag >= 10000:
|
|
|
-# port_flag = 0
|
|
|
-# globals().update({port_flag_name: port_flag})
|
|
|
-#
|
|
|
-# if interface_type == "ocr":
|
|
|
-# log("port_pool " + interface_type + " " + str(port_flag) + " " + ocr_port_list[port_index])
|
|
|
-# return ocr_port_list[port_index]
|
|
|
-# elif interface_type == "otr":
|
|
|
-# log("port_pool " + interface_type + " " + str(port_flag) + " " + otr_port_list[port_index])
|
|
|
-# return otr_port_list[port_index]
|
|
|
-# else:
|
|
|
-# log("port_pool " + interface_type + " " + str(port_flag) + " " + soffice_port_list[port_index])
|
|
|
-# return soffice_port_list[port_index]
|
|
|
-
|
|
|
-
|
|
|
if __name__ == "__main__":
|
|
|
_global._init()
|
|
|
set_flask_global()
|