python-diskcache icon indicating copy to clipboard operation
python-diskcache copied to clipboard

diskcache unconditionally imports django if installed

Open BubuOT opened this issue 7 months ago • 1 comments

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?

BubuOT avatar Jun 03 '25 12:06 BubuOT

There are a couple solutions I can think of that don't involve changing diskcache:

  1. (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.)
  2. (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.

akx avatar Jul 18 '25 06:07 akx