浏览代码

use global thread pool executor

justheuristic 5 年之前
父节点
当前提交
d013546c70
共有 1 个文件被更改,包括 5 次插入11 次删除
  1. 5 11
      tesseract/utils/threading.py

+ 5 - 11
tesseract/utils/threading.py

@@ -1,21 +1,15 @@
-from concurrent.futures import Future, as_completed
+import os
+from concurrent.futures import Future, ThreadPoolExecutor, as_completed
 import time
-from threading import Thread
 from typing import Optional, List
 
+GLOBAL_EXECUTOR = ThreadPoolExecutor(max_workers=os.environ.get("TESSERACT_THREADS", float('inf')))
+
 
 def run_in_background(func: callable, *args, **kwargs) -> Future:
     """ run func(*args, **kwargs) in background and return Future for its outputs """
-    future = Future()
-
-    def _run():
-        try:
-            future.set_result(func(*args, **kwargs))
-        except BaseException as e:
-            future.set_exception(e)
 
-    Thread(target=_run).start()
-    return future
+    return GLOBAL_EXECUTOR.submit(func, *args, **kwargs)
 
 
 def run_forever(func: callable, *args, **kwargs):