Browse Source

Shutdown the executor after compilation is done

Avoid keeping the executor threads around while the process is running.
Masen Furer 1 năm trước cách đây
mục cha
commit
e1f631649f
1 tập tin đã thay đổi với 18 bổ sung5 xóa
  1. 18 5
      reflex/app_module_for_backend.py

+ 18 - 5
reflex/app_module_for_backend.py

@@ -12,11 +12,22 @@ if "app" != constants.CompileVars.APP:
 
 app_module = get_app(reload=False)
 app = getattr(app_module, constants.CompileVars.APP)
-compile_future = ThreadPoolExecutor(max_workers=1).submit(app.compile_)
-compile_future.add_done_callback(
-    # Force background compile errors to print eagerly
-    lambda f: f.result()
-)
+_executor = ThreadPoolExecutor(max_workers=1)
+
+
+def _done(executor: ThreadPoolExecutor):
+    def _cb(f):
+        # Do not leak file handles from the executor itself
+        executor.shutdown(wait=False)
+        # Force background compile errors to print eagerly
+        print("compile done", f.result(), executor)
+
+    return _cb
+
+
+compile_future = _executor.submit(app.compile_)
+compile_future.add_done_callback(_done(_executor))
+
 # Wait for the compile to finish in prod mode to ensure all optional endpoints are mounted.
 if is_prod_mode():
     compile_future.result()
@@ -24,6 +35,8 @@ if is_prod_mode():
 # ensure only "app" is exposed.
 del app_module
 del compile_future
+del _done
+del _executor
 del get_app
 del get_compiled_app
 del is_prod_mode