I think a lot of usages of asgiref.local.Local looks a lot like with ContextVars:
def do_thing():
set_local(some_val)
do_thing_using_local()
reset_local()
contextvar.ContextVar has a helpful pair of utils for thsis, set and reset:
var = ContextVar('var')
token = var.set('new value')
# do stuff using var
var.reset(token)
# reverse change
I do feel like Local having something like .set(k, v)/.get(k)/.reset(token) makes sense, especially given that there's a ContextVar under all of this stuff already.
def set(k, v): ... # set local attr k to v, return a token that can be used to reset
def get(k): getattr(self, k) # more for symmetry with set than anything
def reset(token): ... # reset the state of the _entire Local_ to the state before set
Having said all that I haven't thought about this too deeply, and it might not actually be doable in all use cases. In particular this might not actually play well with context switchups that I saw in #534.
I think a lot of usages of
asgiref.local.Locallooks a lot like withContextVars:contextvar.ContextVarhas a helpful pair of utils for thsis,setandreset:I do feel like
Localhaving something like.set(k, v)/.get(k)/.reset(token)makes sense, especially given that there's aContextVarunder all of this stuff already.Having said all that I haven't thought about this too deeply, and it might not actually be doable in all use cases. In particular this might not actually play well with context switchups that I saw in #534.