From 949fd5f00d1cd97df8cd117c1e5005a1d27726f8 Mon Sep 17 00:00:00 2001 From: Pablo Galindo Date: Tue, 8 Nov 2022 13:57:00 +0000 Subject: [PATCH] Clarify that every thread has its own default context in contextvars --- Doc/library/contextvars.rst | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Doc/library/contextvars.rst b/Doc/library/contextvars.rst index 08a7c7d74eab97a..1468fb6ae33dbec 100644 --- a/Doc/library/contextvars.rst +++ b/Doc/library/contextvars.rst @@ -14,6 +14,12 @@ and work with *Context Variables*. The :func:`~contextvars.copy_context` function and the :class:`~contextvars.Context` class should be used to manage the current context in asynchronous frameworks. +.. note:: + By default, every thread will have a different default + :class:`~contextvars.Context`. This means that a + :class:`~contextvars.ContextVar` can generally be used as a drop-in + replacement for a :func:`threading.local` variable. + Context managers that have state should use Context Variables instead of :func:`threading.local()` to prevent their state from bleeding to other code unexpectedly, when used in concurrent code.