"""Build-time warm-up (Docker variant): the Space's own preload at import of app.py (every arm / tier on the interdigital example: the prior's numba kernels, the NN kernel, the surrogates), so the image carries the compiled cache; then (flagship) the examples' results baked for the response cache. Each step runs in its own process: the preload must succeed; the bake may fail (e.g. the builder runs out of memory), and the image is then built without it (the Space computes the examples on demand).""" import os import resource import subprocess import sys import time from pathlib import Path os.environ["TLSQ_PRELOAD"] = "1" t = time.perf_counter() subprocess.run([sys.executable, "-c", "import app"], check=True) # noqa: S603 (app.py preloads at import; no server) print("warm.py: preload and cache population", round(time.perf_counter() - t, 1), "s", flush=True) t = time.perf_counter() r = subprocess.run([sys.executable, "-c", "import app\nif hasattr(app, 'bake'):\n app.bake()"]) # noqa: S603 peak = resource.getrusage(resource.RUSAGE_CHILDREN).ru_maxrss / 2**10 if r.returncode: Path(os.environ.get("TLSQ_RESPONSE_BAKE", "bake/respcache.pkl")).unlink(missing_ok=True) print(f"warm.py: bake FAILED (exit {r.returncode}, e.g. out of memory): the image is built without baked example results", flush=True) print("warm.py: bake step", round(time.perf_counter() - t, 1), "s, peak RSS of a step", round(peak), "MB", flush=True)