Add container docs

This commit is contained in:
Roman Mogylatov 2021-03-03 08:23:43 -05:00
parent b3b44f628a
commit f52ef6f090
2 changed files with 31 additions and 0 deletions

View File

@ -18,6 +18,14 @@ Method ``.reset_singletons()`` also resets singletons in sub-containers: ``provi
:lines: 3-
:emphasize-lines: 21
You can use ``.reset_singletons()`` method with a context manager. Singletons will be reset on
both entering and exiting a context.
.. literalinclude:: ../../examples/containers/reset_singletons_with.py
:language: python
:lines: 3-
:emphasize-lines: 14-15
See also: :ref:`singleton-provider`.
.. disqus::

View File

@ -0,0 +1,23 @@
"""Container reset singletons context manager example."""
from dependency_injector import containers, providers
class Container(containers.DeclarativeContainer):
service = providers.Singleton(object)
if __name__ == '__main__':
container = Container()
service1 = container.service()
with container.reset_singletons():
service2 = container.service()
service3 = container.service()
assert service1 is not service2
assert service2 is not service3
assert service3 is not service1