diff --git a/docs/source/user_guide/config.rst b/docs/source/user_guide/config.rst index 5cc28eb2bf..d537b154e6 100644 --- a/docs/source/user_guide/config.rst +++ b/docs/source/user_guide/config.rst @@ -123,6 +123,10 @@ FiftyOne supports the configuration options described below: | | | | operations such reading/writing large datasets or activating FiftyOne | | | | | Brain methods on datasets. | +-------------------------------+-------------------------------------+-------------------------------+----------------------------------------------------------------------------------------+ +| `singleton_cache` | `FIFTYONE_SINGLETON_CACHE` | `True` | Whether to treat :class:`Dataset `, | +| | | | :class:`Sample `, and | +| | | | :class:`Frame ` instances as singletons. | ++-------------------------------+-------------------------------------+-------------------------------+----------------------------------------------------------------------------------------+ | `timezone` | `FIFTYONE_TIMEZONE` | `None` | An optional timezone string. If provided, all datetimes read from FiftyOne datasets | | | | | will be expressed in this timezone. See :ref:`this section ` for | | | | | more information. | @@ -182,6 +186,7 @@ and the CLI: "plugins_dir": null, "requirement_error_level": 0, "show_progress_bars": true, + "singleton_cache": true, "timezone": null } @@ -231,6 +236,7 @@ and the CLI: "plugins_dir": null, "requirement_error_level": 0, "show_progress_bars": true, + "singleton_cache": true, "timezone": null } diff --git a/fiftyone/core/config.py b/fiftyone/core/config.py index b2bf8d4050..4aa663fabe 100644 --- a/fiftyone/core/config.py +++ b/fiftyone/core/config.py @@ -244,6 +244,12 @@ def __init__(self, d=None): env_var="FIFTYONE_MAX_PROCESS_POOL_WORKERS", default=None, ) + self.singleton_cache = self.parse_bool( + d, + "singleton_cache", + env_var="FIFTYONE_SINGLETON_CACHE", + default=True, + ) self._init() diff --git a/fiftyone/core/singletons.py b/fiftyone/core/singletons.py index 9068dfa44e..b7103578f3 100644 --- a/fiftyone/core/singletons.py +++ b/fiftyone/core/singletons.py @@ -8,6 +8,8 @@ from collections import defaultdict import weakref +import fiftyone as fo + class DatasetSingleton(type): """Singleton metaclass for :class:`fiftyone.core.dataset.Dataset`. @@ -44,7 +46,8 @@ def __call__(cls, name=None, _create=True, *args, **kwargs): name=name, _create=_create, *args, **kwargs ) - cls._instances[name] = instance + if fo.config.singleton_cache: + cls._instances[name] = instance return instance @@ -107,7 +110,8 @@ def __new__(metacls, *args, **kwargs): return cls def _register_instance(cls, obj): - cls._instances[obj._doc.collection_name][obj.id] = obj + if fo.config.singleton_cache: + cls._instances[obj._doc.collection_name][obj.id] = obj def _get_instance(cls, doc): try: @@ -252,9 +256,10 @@ def __new__(metacls, *args, **kwargs): return cls def _register_instance(cls, obj): - cls._instances[obj._doc.collection_name][obj.sample_id][ - obj.frame_number - ] = obj + if fo.config.singleton_cache: + cls._instances[obj._doc.collection_name][obj.sample_id][ + obj.frame_number + ] = obj def _get_instance(cls, doc): try: