Cache and restart reuse#
Compiled artifacts are cached on disk. After a Python kernel restart, you can import the cached module again without recompiling (as long as the runtime fingerprint matches).
This example demonstrates:
compiling a snippet
listing cached entries
importing the same module from the cache by key
(optional) showing cache stats
# Authors: The scikit-plots developers
# SPDX-License-Identifier: BSD-3-Clause
def _print_entry_summary(entries: list[Any], limit: int = 5) -> None:
"""Print a stable cache listing summary."""
print("Cache entries:", len(entries))
if not entries:
return
# Print last few entries by whatever order list_cached returns.
tail = entries[-limit:]
print(f"Last {len(tail)} entries:")
for e in tail:
# CacheEntry is expected to have these (defaults exist).
print(" - key:", getattr(e, "key", None))
print(" module_name:", getattr(e, "module_name", None))
print(" artifact_path:", getattr(e, "artifact_path", None))
print(" created_utc:", getattr(e, "created_utc", None))
Generate python Module from python#
report = cython.check_build_prereqs(numpy=False)
if not report.get('cython', {}).get('ok'):
print("Skipping compilation because build prerequisites are missing.")
problems = report.get("problems", [])
if problems:
print("Problems:", problems)
else:
# 1) Compile a snippet and capture its deterministic cache key.
r = cython.compile_and_load_result("def f(int n):\n return n*n\n", profile="fast-debug", verbose=0)
key = r.key
print("\nBuildResult:")
print(" module_name:", r.module_name)
print(" key :", r.key)
print(" artifact :", r.artifact_path)
print(" used_cache :", r.used_cache)
# 2) List cache entries (safe if empty).
entries = cython.list_cached()
_print_entry_summary(entries)
# Optional: cache stats snapshot (if available).
if hasattr(cython, "cache_stats"):
print("\nCache stats snapshot:")
print(cython.cache_stats())
# 3) Import the same module by key (restart-safe mechanism).
# Use import_cached_result if available so we can inspect metadata.
if hasattr(cython, "import_cached_result"):
r2 = cython.import_cached_result(key)
m2 = r2.module
print("\nImported from cache (result API):")
print(" module_name:", r2.module_name)
print(" used_cache :", r2.used_cache)
print(" artifact :", r2.artifact_path)
else:
m2 = cython.import_cached(key)
print("\nImported from cache (module API):", m2.__name__)
# 4) Verify correctness strictly.
out = m2.f(11)
print("import_cached(key).f(11) =", out)
print("Expected:", 121)
print("Correct:", out == 121)
BuildResult:
module_name: scikitplot_cython_5101778495713d4e
key : 5101778495713d4e751154df264a1d2d9ef072321d4de8035a1fa2137dc6795f
artifact : /home/circleci/.cache/scikitplot/cython/5101778495713d4e751154df264a1d2d9ef072321d4de8035a1fa2137dc6795f/scikitplot_cython_5101778495713d4e.cpython-312-x86_64-linux-gnu.so
used_cache : True
Cache entries: 6
Last 5 entries:
- key: 3dc5e64742a1231116f647636f77f637ad9cd5241d2e02670f32d164c0a7851f
module_name: scikitplot_cython_3dc5e64742a12311
artifact_path: /home/circleci/.cache/scikitplot/cython/3dc5e64742a1231116f647636f77f637ad9cd5241d2e02670f32d164c0a7851f/scikitplot_cython_3dc5e64742a12311.cpython-312-x86_64-linux-gnu.so
created_utc: 2026-11-08T15:17:34Z
- key: 431feaa7063234ee8a6838e76f05cb8601cb5280e03c9f41dd1fe1b1815b8f5d
module_name: scikitplot_cython_431feaa7063234ee
artifact_path: /home/circleci/.cache/scikitplot/cython/431feaa7063234ee8a6838e76f05cb8601cb5280e03c9f41dd1fe1b1815b8f5d/scikitplot_cython_431feaa7063234ee.cpython-312-x86_64-linux-gnu.so
created_utc: 2026-11-08T15:17:29Z
- key: 5101778495713d4e751154df264a1d2d9ef072321d4de8035a1fa2137dc6795f
module_name: scikitplot_cython_5101778495713d4e
artifact_path: /home/circleci/.cache/scikitplot/cython/5101778495713d4e751154df264a1d2d9ef072321d4de8035a1fa2137dc6795f/scikitplot_cython_5101778495713d4e.cpython-312-x86_64-linux-gnu.so
created_utc: 2026-11-08T15:17:28Z
- key: 763d0340331f34c51a3644cd404047f4e1fc6c7df56c4db4517ce55ce912a3f3
module_name: scikitplot_cython_763d0340331f34c5
artifact_path: /home/circleci/.cache/scikitplot/cython/763d0340331f34c51a3644cd404047f4e1fc6c7df56c4db4517ce55ce912a3f3/scikitplot_cython_763d0340331f34c5.cpython-312-x86_64-linux-gnu.so
created_utc: 2026-11-08T15:17:32Z
- key: f4925ad63fe6725e190286c3ff562ce14ee1ce364b9cc64a4c9b9e6807b06c1d
module_name: scikitplot_cython_f4925ad63fe6725e
artifact_path: /home/circleci/.cache/scikitplot/cython/f4925ad63fe6725e190286c3ff562ce14ee1ce364b9cc64a4c9b9e6807b06c1d/scikitplot_cython_f4925ad63fe6725e.cpython-312-x86_64-linux-gnu.so
created_utc: 2026-11-08T15:17:30Z
Cache stats snapshot:
CacheStats(cache_root=PosixPath('/home/circleci/.cache/scikitplot/cython'), n_modules=6, n_packages=0, total_bytes=4471516, pinned_aliases=1, pinned_keys=1, newest_mtime_utc='2026-11-08T15:17:36Z', oldest_mtime_utc='2026-11-08T15:17:29Z')
Imported from cache (result API):
module_name: scikitplot_cython_5101778495713d4e
used_cache : True
artifact : /home/circleci/.cache/scikitplot/cython/5101778495713d4e751154df264a1d2d9ef072321d4de8035a1fa2137dc6795f/scikitplot_cython_5101778495713d4e.cpython-312-x86_64-linux-gnu.so
import_cached(key).f(11) = 121
Expected: 121
Correct: True
Total running time of the script: (0 minutes 0.017 seconds)
Related examples
Multi-file builds: .pxi includes and external headers
Multi-file builds: .pxi includes and external headers