mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-11-25 11:04:01 +03:00
Add container docs
This commit is contained in:
parent
b3b44f628a
commit
f52ef6f090
|
@ -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::
|
||||
|
|
23
examples/containers/reset_singletons_with.py
Normal file
23
examples/containers/reset_singletons_with.py
Normal 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
|
Loading…
Reference in New Issue
Block a user