Skip to content

Feature Request: Local.set/Local.reset #548

@rtpg

Description

@rtpg

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions