5

I'm trying to use the new contextvars library (https://docs.python.org/3/library/contextvars.html) to make certain values available across modules in an async context, similar to ContextVars across modules, and was wondering if there was a way of fetching context variables by name or identifier from within a given execution context without importing the context variable directly.

Ideally, I would like to do something like the following:

from _contextvars import ContextVar

language = ContextVar('language')
language.set('en-US')

And in a separate module, which may serve a web request or fetch a translation based on the language that was set:

from _contextvars import copy_context

ctx = copy_context()

if 'language' in ctx:
  lang_context_var = ctx.get('language') # get the context variable
  lang_value = lang_context_var.get() # get the value

I realise the API for the contextvars library has a mapping between a ContextVar object and value and not string to value so this lookup can't be done, but I find that needing to do a from module_with_context_var import language a bit messy and prone to being spread across a codebase rather than a single wrapper for the current context , which seems like what copy_context() should be.

Any pointers in a good way to structure context variable fetching and setting in a central location which also works with co-routines/async programming would be much appreciated!

vogelier
  • 51
  • 1

0 Answers0