You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
diskcache unconditionally imports django if installed聽#347
Hi, this is bit of a niche issue and I apologize in advance 馃槄. I do think there's some general merit to improving this though.
Background: We use diskcache on an embedded system based on a Raspberrypi compute module 4. We also have a django app there, but that is a separate process and we don't actually use the django diskcache based cache backend.
I just discovered that when django is installed diskcache unconditionally imports some django machinery for setting up it's DjangoCache. This causes an additonal 500ms startup time for a simple process that uses diskache compared to when django is not present on the system.
Doing the same test on my development machine there difference is < 30ms. But any python process that uses diskcache also uses about 10MB more memory just by django being present.
$ /usr/bin/time -v python -c "import diskcache"
Command being timed: "python -c import diskcache"
[...]
Maximum resident set size (kbytes): 24640
[...]
$ pip uninstall django
Found existing installation: Django 5.2.1
Uninstalling Django-5.2.1:
[...]
Successfully uninstalled Django-5.2.1
$ /usr/bin/time -v python -c "import diskcache"
Command being timed: "python -c import diskcache"
[...]
Maximum resident set size (kbytes): 14552
[...]
Exit status: 0
I'm not sure what the best solution here is but maybe the DjangoCache can be lazily imported when the class is actually imported from another module?
There are a couple solutions I can think of that don't involve changing diskcache:
(Simple, clean:) Use different Python venvs, one with Django, one without. (If you use uv for managing the venvs, it'll try hard to hard-link library files to share them between venvs, so there's no much extra space used.)
(Hacky:) In your non-django-using process, run sys.modules["django"] = None before ever importing diskcache; this'll cause the import django... in diskcache to raise ModuleNotFoundError: import of django halted; None in sys.modules, which'll be caught by the except Exception: in diskcache's __init__.
Testing this locally, without the patch, env PYTHONPROFILEIMPORTTIME=1 uv run main.py says importing diskcache takes 40ms, with it 12ms, so it does do a thing.
Activity
akx commentedon Jul 18, 2025
There are a couple solutions I can think of that don't involve changing
diskcache:sys.modules["django"] = Nonebefore ever importingdiskcache; this'll cause theimport django...indiskcacheto raiseModuleNotFoundError: import of django halted; None in sys.modules, which'll be caught by theexcept Exception:indiskcache's__init__.env PYTHONPROFILEIMPORTTIME=1 uv run main.pysays importingdiskcachetakes 40ms, with it 12ms, so it does do a thing.