Dynaconf returns attributes very slowly when accessed repeatedly. In the code below, retrieving settingsmon.mode
in a loop 40,000 times takes about 12 seconds:
from datetime import datetime
from dynaconf import Dynaconf
settings = Dynaconf(
envvar_prefix="APP",
settings_files=['config.toml']
)
print(f'Started: {datetime.now().strftime("%d.%b %Y %H:%M:%S")}')
for i in range(40000):
x = settingsmon.mode
print(f'End: {datetime.now().strftime("%d.%b %Y %H:%M:%S")}')
Does dynaconf have a caching mechanism to speed up attribute access? If so, why isn’t it enabled by default and how can I enable it?
Furthermore,
Info from CProfiler:
30477334 function calls (29574957 primitive calls) in 22.655 seconds
Ordered by: cumulative time
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.084 0.084 22.655 22.655 D:app\xxx\2.py:12(fun1)
20000 0.199 0.000 22.302 0.001 D:app\venv\Lib\site-packages\dynaconf\base.py:119(getattr)
467437/127422 0.467 0.000 21.707 0.000 {built-in method builtins.getattr}
503290/121931 0.740 0.000 21.108 0.000 D:app\venv\Lib\site-packages\dynaconf\utils\boxing.py:16(evaluate)
120000 0.426 0.000 19.421 0.000 D:app\venv\Lib\site-packages\dynaconf\utils\boxing.py:35(getattr)
265065/84373 0.732 0.000 18.050 0.000 D:app\venv\Lib\site-packages\dynaconf\base.py:306(getattribute)
20464 0.177 0.000 17.703 0.001 D:app\venv\Lib\site-packages\dynaconf\base.py:458(get)
220000 1.153 0.000 16.322 0.000 D:app\venv\Lib\site-packages\dynaconf\vendor\box\box.py:164(getattr)
223290 0.165 0.000 12.272 0.000 D:app\venv\Lib\site-packages\dynaconf\utils\__init__.py:435(recursively_evaluate_lazy_format)
223302/223290 0.194 0.000 12.107 0.000 D:app\venv\Lib\site-packages\dynaconf\utils\__init__.py:446(_recursively_evaluate_lazy_format)
81931 0.166 0.000 8.629 0.000 D:app\venv\Lib\site-packages\dynaconf\utils\boxing.py:78(get)
81931 0.077 0.000 8.355 0.000 D:app\venv\Lib\site-packages\dynaconf\vendor\box\box.py:98(get)
381931 4.968 0.000 8.199 0.000 D:app\venv\Lib\site-packages\dynaconf\utils\__init__.py:486(find_the_correct_casing)
301359 0.979 0.000 7.843 0.000 D:app\venv\Lib\site-packages\dynaconf\utils\boxing.py:43(getitem)
200173 3.560 0.000 7.192 0.000 D:app\venv\Lib\site-packages\dynaconf\vendor\box\box.py:280(_safe_attr)
200189 1.837 0.000 2.662 0.000 D:app\venv\Lib\site-packages\dynaconf\vendor\box\box.py:128(box_config)
10845712 1.840 0.000 1.840 0.000 {method 'lower' of 'str' objects}
501359 1.244 0.000 1.447 0.000 D:app\venv\Lib\site-packages\dynaconf\vendor\box\box.py:150(__getitem)