contextvars.copy_context()
contextvars.copy_context() is a Python function in the ContextVars Module category. Returns a copy of the current context (all context variables and their values). The syntax is contextvars.copy_context(). Returns: Context object. A typical example: ctx = contextvars.copy_context()
for var, value in ctx.items():
print(var.name, "=", value). A close sibling is contextvars.ContextVar(), which creates a context variable that has separate values in different async contexts. A close sibling is ContextVar.get(), which returns the value of the context variable for the current context. A close sibling is ContextVar.set(), which sets the value of the context variable for the current context. A close sibling is ContextVar.reset(), which resets the context variable to its previous value using the token from set(). More about this category: Context variables for async — ContextVar, copy_context, Context. Related ContextVars Module entries: contextvars.ContextVar(), ContextVar.get(), ContextVar.set(), ContextVar.reset(). This page is part of the free Python Reference documentation covering 978+ functions, methods, and modules with examples and parameter details.