From 650ee014ec36850d031629c7ed35b67ac1e9e0ea Mon Sep 17 00:00:00 2001 From: Roman Mogilatov Date: Fri, 4 Sep 2015 02:33:15 +0300 Subject: [PATCH] Implement thread-safety --- README.rst | 1 + VERSION | 2 +- dependency_injector/providers.py | 6 ++++-- dependency_injector/utils.py | 5 +++++ docs/index.rst | 1 + docs/main/changelog.rst | 4 ++++ docs/providers/index.rst | 3 +++ 7 files changed, 19 insertions(+), 3 deletions(-) diff --git a/README.rst b/README.rst index 9e2dbf45..a0186a95 100644 --- a/README.rst +++ b/README.rst @@ -39,6 +39,7 @@ framework: - Easy, smart, pythonic style. - Obvious, clear structure. - Memory efficiency. +- Thread safety. - Semantic versioning. Main idea of *Dependency Injector* is to keep dependencies under control. diff --git a/VERSION b/VERSION index 2003b639..965065db 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.9.2 +0.9.3 diff --git a/dependency_injector/providers.py b/dependency_injector/providers.py index 40d4ec4c..39b27476 100644 --- a/dependency_injector/providers.py +++ b/dependency_injector/providers.py @@ -9,6 +9,7 @@ from .utils import is_kwarg_injection from .utils import is_attribute_injection from .utils import is_method_injection from .utils import get_injectable_kwargs +from .utils import GLOBAL_LOCK from .errors import Error @@ -162,8 +163,9 @@ class Singleton(Provider): def _provide(self, *args, **kwargs): """Return provided instance.""" - if not self._instance: - self._instance = self._factory(*args, **kwargs) + with GLOBAL_LOCK: + if not self._instance: + self._instance = self._factory(*args, **kwargs) return self._instance def reset(self): diff --git a/dependency_injector/utils.py b/dependency_injector/utils.py index f3ed3467..f737be81 100644 --- a/dependency_injector/utils.py +++ b/dependency_injector/utils.py @@ -1,10 +1,15 @@ """Utils module.""" +import threading + import six from .errors import Error +GLOBAL_LOCK = threading.RLock() + + def is_provider(instance): """Check if instance is provider instance.""" return (not isinstance(instance, six.class_types) and diff --git a/docs/index.rst b/docs/index.rst index 8ded4413..d4d648ed 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -39,6 +39,7 @@ framework: - Easy, smart, pythonic style. - Obvious, clear structure. - Memory efficiency. +- Thread safety. - Semantic versioning. Main idea of *Dependency Injector* is to keep dependencies under control. diff --git a/docs/main/changelog.rst b/docs/main/changelog.rst index 550aac63..87e70504 100644 --- a/docs/main/changelog.rst +++ b/docs/main/changelog.rst @@ -13,6 +13,10 @@ Development version - No featues. +0.9.3 +----- +- Implement thread-safety. + 0.9.2 ----- - Add minor refactorings. diff --git a/docs/providers/index.rst b/docs/providers/index.rst index f5333c24..62c57abc 100644 --- a/docs/providers/index.rst +++ b/docs/providers/index.rst @@ -12,6 +12,9 @@ Current documentation section consists from description of standard providers library and some useful information like overriding of providers and writing custom providers. +All providers are validated in multithreading environment and considered to +be thread-safe. + .. toctree:: :maxdepth: 2