Application Context#
FastStreams has its own Dependency Injection container - Context, used to store application runtime objects and variables.
With this container, you can access both application scope and message processing scope objects. This functionality is similar to Depends
usage.
But, with the Annotated
Python feature usage, it is much closer to @pytest.fixture
.
Usages#
By default, the context is available in the same place as Depends
:
- at lifespan hooks
- message subscribers
- nested dependencies
Tip
Fields obtained from the Context
are editable, so editing them in a function means editing them everywhere.
Compatibility with Regular Functions#
To use context in other functions, use the @apply_types
decorator. In this case, the context of the called function will correspond to the context of the event handler from which it was called.
In the example above, we did not pass the logger
function at calling it; it was placed outside of context.