jack 2 месяцев назад
Родитель
Сommit
01a9d73f6c
2 измененных файлов с 19 добавлено и 2 удалено
  1. 1 0
      app/core/runner/thread_runner.py
  2. 18 2
      app/libs/thread_executor.py

+ 1 - 0
app/core/runner/thread_runner.py

@@ -219,6 +219,7 @@ class ThreadRunner:
             # 为减少线程同步逻辑,依次处理内/外 tool_call 调用
             # 为减少线程同步逻辑,依次处理内/外 tool_call 调用
             if internal_tool_calls:
             if internal_tool_calls:
                 try:
                 try:
+
                     ## 线程执行有问题 可以改成异步
                     ## 线程执行有问题 可以改成异步
                     tool_calls_with_outputs = run_with_executor(
                     tool_calls_with_outputs = run_with_executor(
                         executor=ThreadRunner.tool_executor,
                         executor=ThreadRunner.tool_executor,

+ 18 - 2
app/libs/thread_executor.py

@@ -18,7 +18,9 @@ def get_executor_for_config(worker_num: int, thread_name_prefix: str) -> Executo
         Executor: A ThreadPoolExecutor instance.
         Executor: A ThreadPoolExecutor instance.
 
 
     """
     """
-    executor = ThreadPoolExecutor(max_workers=worker_num, thread_name_prefix=thread_name_prefix)
+    executor = ThreadPoolExecutor(
+        max_workers=worker_num, thread_name_prefix=thread_name_prefix
+    )
     atexit.register(executor.shutdown, wait=False)
     atexit.register(executor.shutdown, wait=False)
     return executor
     return executor
 
 
@@ -38,7 +40,7 @@ def run_with_executor(executor: Executor, func, tasks: List, timeout: int):
 
 
     Raises:
     Raises:
         Exception: If any of the tasks raise an exception.
         Exception: If any of the tasks raise an exception.
-    """
+
     futures = [executor.submit(lambda args: func(*args), task) for task in tasks]
     futures = [executor.submit(lambda args: func(*args), task) for task in tasks]
     done, _ = concurrent.futures.wait(futures, return_when=concurrent.futures.FIRST_EXCEPTION, timeout=timeout)
     done, _ = concurrent.futures.wait(futures, return_when=concurrent.futures.FIRST_EXCEPTION, timeout=timeout)
 
 
@@ -50,3 +52,17 @@ def run_with_executor(executor: Executor, func, tasks: List, timeout: int):
         if future.done():
         if future.done():
             results.append(future.result())
             results.append(future.result())
     return results
     return results
+    """
+
+    results = []
+    # Iterate over tasks and execute them sequentially
+    for task in tasks:
+        future = executor.submit(lambda args: func(*args), task)
+        # Wait for the task to complete (with timeout)
+        try:
+            result = future.result(timeout=timeout)
+            results.append(result)
+        except Exception as e:
+            print(e)
+
+    return results