refactor: load firebase_admin lazily#279
Conversation
|
@xtrinch I just pushed |
|
@xtrinch Sorry, I missed one error. I just fixed that one as well. |
|
@xtrinch I'm not sure how to fix the 3.7 action. onnx/tensorflow-onnx#2376 suggests limits 3.7 to ubuntu 22.04, maybe that's the solution? Let me know if there's anything I can do to help. |
|
Python 3.7 has reached its end of life, I believe we can safely remove it. |
According to In isolation, that probably doesn't sound like a lot, but I work on a project with a lot of different dependencies, so I'm always looking for ways to improve the import/setup speed of those dependencies. We tend to accumulate a lot of performance benefit by optimizing many of our different imports, so every little bit counts to the overall goal of improving django start time. For example, if we can improve the import time of 5 different dependencies that each take about 100ms to import and Thanks for your time and consideration. |
|
@jonesnc This latest release 2.3.1 bump up the ceiling for |
|
It looks like my project will take some work to update to |
|
@ahmedbilal I finally did some testing by upgrading to version 2.3.1, and found that while removing the I'll go ahead and fix the branch conflicts. |
|
Hi @ahmedbilal @xtrinch, I pushed some updates here. I added a new The One thing to note: the cached app uses a module-level global without locking, so there's a potential race condition if multiple threads hit the first FCM call simultaneously. In practice this probably just means the callable gets invoked more than once before the cache is set. Let me know if you'd like me to add thread-safe locking if this is not acceptable. Please let me know if you have any suggestions or concerns about this approach. Thanks for your time. |
|
@ahmedbilal @xtrinch Any updates on this? |
|
@jonesnc Alright I have cleaned this up so we dont do lazy imports all across the board, wrapped them in some wrapper functions, renamed the setting parameter, and I think it's good to go. I think the use cases justify this even though it does kind of increase complexity of the codebase. Do a quick check please |
Add INITIALIZE_APP_CALLABLE setting that defers Firebase initialization until first use. The callable is invoked lazily and the result is cached for subsequent calls.
a9fc755 to
4375c26
Compare
|
@jonesnc @xtrinch Not sure whether this would help this cause or not, but see https://docs.python.org/3.15/whatsnew/3.15.html#pep-810-explicit-lazy-imports |
|
@ahmedbilal This will only be useful once Python ≥3.15 becomes the baseline (e.g. in LTS environments). Right now, it doesn’t help us. |
|
I'm happy to just wait until Oct 1, 2026, when Python 3.15 is released. Would that be a good time to revisit this, or do you want to wait for a later date? |
|
@jonesnc Yes, that works, but we'd need to make sure it still works for earlier python versions, even if make lazy imports work for python 3.15. |
__lazy_modules__ = ["json", "pathlib"]
import json # lazy
import os # still eagerSo we could use |
Changes
Lazily import
firebase_adminand its modules because we've found it's one of the slower modules to import in our Django projects. For type annotations, thefirebase_adminis only imported after checkingtyping.TYPE_CHECKING.Please let me know if you have any suggestions or feedback on this, I'd be happy to implement them.
Thanks for this package, I think it's great!
Tests
Test session starts (platform: linux, Python 3.10.12, pytest 8.2.2, pytest-sugar 1.0.0) django: version: 4.2.19, settings: tests.settings.default (from ini) rootdir: /home/nathanjones/Projects/fcm-django configfile: pyproject.toml plugins: sugar-1.0.0, anyio-3.6.2, mock-3.14.1, django-4.11.1, asyncio-0.14.0, Faker-15.3.4, typeguard-2.13.3 collected 16 items tests/test_admin.py ✓✓ 12% █▍ tests/test_api_rest_framework.py ✓✓✓ 31% ███▎ tests/test_api_tastypie.py ✓ 38% ███▊ tests/test_models.py ✓✓✓✓✓✓✓✓✓✓ 100% ██████████ Results (3.49s): 16 passed