Add the ability to lazily import and construct registries.

Review Request #10511 — Created April 4, 2019 and submitted

Information

Djblets
release-2.0.x

Reviewers

Registry instances are commonly constructed in the __init__.py of a
module, but this can interfere with the Django initialization process if
the registry's module or population logic requires direct or indirect
access to modules containing models or some other Django apps.

The solution to this is to lazily construct the registries on access.
This can be done through SimpleLazyObject, which will construct an
instance on first access and then forward on all operations to it. To
simplify usage of this, this change introduces a
lazy_import_registry() method, which will set up a function to import
and construct of the registry and return a SimpleLazyObject with that
function.

This should be usable in any place where a registry needs to be
constructed. It can also help speed up startup times when working with
more expensive registries.

Made use of this in Review Board. All unit tests pass there on all
supported versions of Django.

Summary ID
Add the ability to lazily import and construct registries.
Registry instances are commonly constructed in the `__init__.py` of a module, but this can interfere with the Django initialization process if the registry's module or population logic requires direct or indirect access to modules containing models or some other Django apps. The solution to this is to lazily construct the registries on access. This can be done through `SimpleLazyObject`, which will construct an instance on first access and then forward on all operations to it. To simplify usage of this, this change introduces a `lazy_import_registry()` method, which will set up a function to import and construct of the registry and return a `SimpleLazyObject` with that function. This should be usable in any place where a registry needs to be constructed. It can also help speed up startup times when working with more expensive registries.
31bb775f06d964351acb9c877f77c22551bfef5c
david
  1. Ship It!
  2. 
      
chipx86
Review request changed

Status: Closed (submitted)

Change Summary:

Pushed to release-2.0.x (af28b05)
Loading...