import concurrent.futures
def glm_batch_translate(req_lists):
# 使用线程池进行并行翻译
items_to_translate = {i: req_lists[i] for i in range(len(req_lists))}
total_items = len(items_to_translate)
# 使用线程池进行翻译
with ThreadPoolExecutor(max_workers=50) as executor:
futures = {executor.submit(glm_translate, value): key for key, value in items_to_translate.items()}
# 创建一个列表来存储翻译结果,初始值为None
translated_items = {key: None for key in items_to_translate}
while futures:
# 等待一段时间,希望在这段时间内能有多个任务完成
time.sleep(3)
# 检查已完成的任务
completed_futures = [future for future in futures if future.done()]
for future in completed_futures:
# result = future.result()
# results.append(result)
key = futures[future]
# 获取翻译结果,并按原始顺序存储
translated_items[key] = future.result()
# print(f"翻译完成: {result}")
futures.pop(future)
# 计算当前进度
current_progress = (total_items - len(futures)) * 100 // total_items
# 打印当前进度
a_function(current_progress)
# 将翻译结果字典按key排序
sorted_translated_items = {k: translated_items[k] for k in sorted(translated_items)}
results = [sorted_translated_items[i] for i in sorted_translated_items]
return results
python多线程翻译获取进度结果
于 2024-06-11 22:45:02 首次发布