Skip to content

Instantly share code, notes, and snippets.

@GreyElaina
Created April 8, 2024 08:58
Show Gist options
  • Select an option

  • Save GreyElaina/d09a6671127a167c771205c74d3d36e0 to your computer and use it in GitHub Desktop.

Select an option

Save GreyElaina/d09a6671127a167c771205c74d3d36e0 to your computer and use it in GitHub Desktop.
from contextvars import ContextVar, Context
import gc
def get_current_context():
local = ContextVar("#local")
token = local.set(1)
for i in gc.get_referents(token):
if isinstance(i, Context):
return i
raise RuntimeError
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment