Shutdown the executor after compilation is done

Avoid keeping the executor threads around while the process is running.
This commit is contained in:
Masen Furer 2024-03-01 12:04:39 -08:00
parent b6cfa110af
commit e1f631649f
No known key found for this signature in database
GPG Key ID: B0008AD22B3B3A95

View File

@ -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