This commit is contained in:
@@ -225,9 +225,34 @@ class TranscriptionTaskViewSet(viewsets.ModelViewSet):
|
||||
eval.save()
|
||||
evaluations_to_process.append(eval)
|
||||
|
||||
# 执行评估
|
||||
for eval_obj in evaluations_to_process:
|
||||
service.evaluate_task(eval_obj)
|
||||
# 执行评估 (改为异步并发执行)
|
||||
# 提取ID列表,避免传递模型对象导致可能的线程问题
|
||||
eval_ids = [e.id for e in evaluations_to_process]
|
||||
|
||||
if eval_ids:
|
||||
import threading
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
|
||||
def run_evaluations_background(ids):
|
||||
# 在后台线程中重新引入依赖
|
||||
from .models import AIEvaluation
|
||||
from .bailian_service import BailianService
|
||||
|
||||
# 为该线程创建独立的服务实例
|
||||
local_service = BailianService()
|
||||
|
||||
# 获取最新的对象
|
||||
target_evals = AIEvaluation.objects.filter(id__in=ids)
|
||||
|
||||
# 使用线程池并发执行
|
||||
# max_workers=4 可以同时处理4个评估请求
|
||||
with ThreadPoolExecutor(max_workers=4) as executor:
|
||||
executor.map(local_service.evaluate_task, target_evals)
|
||||
|
||||
# 启动后台线程,不阻塞当前 HTTP 请求
|
||||
thread = threading.Thread(target=run_evaluations_background, args=(eval_ids,))
|
||||
thread.daemon = True # 设置为守护线程
|
||||
thread.start()
|
||||
|
||||
# 返回该任务的所有评估结果
|
||||
all_evals = AIEvaluation.objects.filter(task=task)
|
||||
|
||||
Reference in New Issue
Block a user