that-depends icon indicating copy to clipboard operation
that-depends copied to clipboard

Increase granularity of context.

Open alexanderlazarev0 opened this issue 4 months ago • 9 comments

Currently, container_context() resets/reinitializes the whole context. This makes it hard to manage resources with different lifetimes. As a theoretical workaround, it is possible to build an initial_context that keeps resources in the scope, but that requires using context internals (starting with _).

Proposal:

  • Allow passing providers to container_context:
async with container_context(container.provider1, container.provider2, ...): 
# resets the context for listed providers.
  • Allow passing containers to container_context:
async with container_context(container1, container2, ...):
# resets the context for context-resources in given containers

Implementation details:

  • Make each ContextResource manage its own context(var) on a class level.
  • container_context now just interacts with these context(var)s to set and reset.

Goals:

  • Maintain compatibility with the current solution:
async with container_context(): # will still reset all contexts across all containers
  • Maintain current functionality with initial_context

@lesnik512 If you approve of this proposal, I will start working on it. I also saw that you make active use of setting the initial_context, if you could provide a functional example I will make sure to test against it when converting functionality.

alexanderlazarev0 avatar Oct 05 '24 08:10 alexanderlazarev0