Hi, This is a third version of PEP 567.
Changes from v2: 1. PyThreadState now references Context objects directly (instead of referencing _ContextData). This fixes out of sync Context.get() and ContextVar.get(). 2. Added a new Context.copy() method. 3. Renamed Token.old_val property to Token.old_value 4. ContextVar.reset(token) now raises a ValueError if the token was created in a different Context. 5. All areas of the PEP were updated to be more precise. Context is *no longer* defined as a read-only or an immutable mapping; ContextVar.get() behaviour is fully defined; the immutability is only mentioned in the Implementation section to avoid confusion; etc. 6. Added a new Examples section. The reference implementation has been updated to include all these changes. The only open question I personally have is whether ContextVar.reset() should be idempotent or not. Maybe we should be strict and raise an error if a user tries to reset a variable more than once with the same token object? Other than that, I'm pretty happy with this version. Big thanks to everybody helping with the PEP! PEP: 567 Title: Context Variables Version: $Revision$ Last-Modified: $Date$ Author: Yury Selivanov <y...@magic.io> Status: Draft Type: Standards Track Content-Type: text/x-rst Created: 12-Dec-2017 Python-Version: 3.7 Post-History: 12-Dec-2017, 28-Dec-2017, 16-Jan-2018 Abstract ======== This PEP proposes a new ``contextvars`` module and a set of new CPython C APIs to support context variables. This concept is similar to thread-local storage (TLS), but, unlike TLS, it also allows correctly keeping track of values per asynchronous task, e.g. ``asyncio.Task``. This proposal is a simplified version of :pep:`550`. The key difference is that this PEP is concerned only with solving the case for asynchronous tasks, not for generators. There are no proposed modifications to any built-in types or to the interpreter. This proposal is not strictly related to Python Context Managers. Although it does provide a mechanism that can be used by Context Managers to store their state. Rationale ========= Thread-local variables are insufficient for asynchronous tasks that execute concurrently in the same OS thread. Any context manager that saves and restores a context value using ``threading.local()`` will have its context values bleed to other code unexpectedly when used in async/await code. A few examples where having a working context local storage for asynchronous code is desirable: * Context managers like ``decimal`` contexts and ``numpy.errstate``. * Request-related data, such as security tokens and request data in web applications, language context for ``gettext``, etc. * Profiling, tracing, and logging in large code bases. Introduction ============ The PEP proposes a new mechanism for managing context variables. The key classes involved in this mechanism are ``contextvars.Context`` and ``contextvars.ContextVar``. The PEP also proposes some policies for using the mechanism around asynchronous tasks. The proposed mechanism for accessing context variables uses the ``ContextVar`` class. A module (such as ``decimal``) that wishes to use the new mechanism should: * declare a module-global variable holding a ``ContextVar`` to serve as a key; * access the current value via the ``get()`` method on the key variable; * modify the current value via the ``set()`` method on the key variable. The notion of "current value" deserves special consideration: different asynchronous tasks that exist and execute concurrently may have different values for the same key. This idea is well-known from thread-local storage but in this case the locality of the value is not necessarily bound to a thread. Instead, there is the notion of the "current ``Context``" which is stored in thread-local storage. Manipulation of the current context is the responsibility of the task framework, e.g. asyncio. A ``Context`` is a mapping of ``ContextVar`` objects to their values. The ``Context`` itself exposes the ``abc.Mapping`` interface (not ``abc.MutableMapping``!), so it cannot be modified directly. To set a new value for a context variable in a ``Context`` object, the user needs to: * make the ``Context`` object "current" using the ``Context.run()`` method; * use ``ContextVar.set()`` to set a new value for the context variable. The ``ContextVar.get()`` method looks for the variable in the current ``Context`` object using ``self`` as a key. It is not possible to get a direct reference to the current ``Context`` object, but it is possible to obtain a shallow copy of it using the ``contextvars.copy_context()`` function. This ensures that the caller of ``Context.run()`` is the sole owner of its ``Context`` object. Specification ============= A new standard library module ``contextvars`` is added with the following APIs: 1. ``copy_context() -> Context`` function is used to get a copy of the current ``Context`` object for the current OS thread. 2. ``ContextVar`` class to declare and access context variables. 3. ``Context`` class encapsulates context state. Every OS thread stores a reference to its current ``Context`` instance. It is not possible to control that reference directly. Instead, the ``Context.run(callable, *args, **kwargs)`` method is used to run Python code in another context. contextvars.ContextVar ---------------------- The ``ContextVar`` class has the following constructor signature: ``ContextVar(name, *, default=_NO_DEFAULT)``. The ``name`` parameter is used for introspection and debug purposes, and is exposed as a read-only ``ContextVar.name`` attribute. The ``default`` parameter is optional. Example:: # Declare a context variable 'var' with the default value 42. var = ContextVar('var', default=42) (The ``_NO_DEFAULT`` is an internal sentinel object used to detect if the default value was provided.) ``ContextVar.get(default=_NO_DEFAULT)`` returns a value for the context variable for the current ``Context``:: # Get the value of `var`. var.get() If there is no value for the variable in the current context, ``ContextVar.get()`` will: * return the value of the *default* argument of the ``get()`` method, if provided; or * return the default value for the context variable, if provided; or * raise a ``LookupError``. ``ContextVar.set(value) -> Token`` is used to set a new value for the context variable in the current ``Context``:: # Set the variable 'var' to 1 in the current context. var.set(1) ``ContextVar.reset(token)`` is used to reset the variable in the current context to the value it had before the ``set()`` operation that created the ``token`` (or to remove the variable if it was not set):: assert var.get(None) is None token = var.set(1) try: ... finally: var.reset(token) assert var.get(None) is None ``ContextVar.reset()`` method is idempotent and can be called multiple times on the same Token object: second and later calls will be no-ops. The method raises a ``ValueError`` if: * called with a token object created by another variable; or * the current ``Context`` object does not match the one where the token object was created. contextvars.Token ----------------- ``contextvars.Token`` is an opaque object that should be used to restore the ``ContextVar`` to its previous value, or to remove it from the context if the variable was not set before. It can be created only by calling ``ContextVar.set()``. For debug and introspection purposes it has: * a read-only attribute ``Token.var`` pointing to the variable that created the token; * a read-only attribute ``Token.old_value`` set to the value the variable had before the ``set()`` call, or to ``Token.MISSING`` if the variable wasn't set before. contextvars.Context ------------------- ``Context`` object is a mapping of context variables to values. ``Context()`` creates an empty context. To get a copy of the current ``Context`` for the current OS thread, use the ``contextvars.copy_context()`` method:: ctx = contextvars.copy_context() To run Python code in some ``Context``, use ``Context.run()`` method:: ctx.run(function) Any changes to any context variables that ``function`` causes will be contained in the ``ctx`` context:: var = ContextVar('var') var.set('spam') def function(): assert var.get() == 'spam' assert ctx[var] == 'spam' var.set('ham') assert var.get() == 'ham' assert ctx[var] == 'ham' ctx = copy_context() # Any changes that 'function' makes to 'var' will stay # isolated in the 'ctx'. ctx.run(function) assert var.get() == 'spam' assert ctx[var] == 'ham' ``Context.run()`` raises a ``RuntimeError`` when called on the same context object from more than one OS thread, or when called recursively. ``Context.copy()`` returns a shallow copy of the context object. ``Context`` objects implement the ``collections.abc.Mapping`` ABC. This can be used to introspect contexts:: ctx = contextvars.copy_context() # Print all context variables and their values in 'ctx': print(ctx.items()) # Print the value of 'some_variable' in context 'ctx': print(ctx[some_variable]) Note that all Mapping methods, including ``Context.__getitem__`` and ``Context.get``, ignore default values for context variables (i.e. ``ContextVar.default``). This means that for a variable *var* that was created with a default value and was not set in the *context*: * ``context[var]`` raises a ``KeyError``, * ``var in context`` returns ``False``, * the variable isn't included in ``context.items()``, etc. asyncio ------- ``asyncio`` uses ``Loop.call_soon()``, ``Loop.call_later()``, and ``Loop.call_at()`` to schedule the asynchronous execution of a function. ``asyncio.Task`` uses ``call_soon()`` to run the wrapped coroutine. We modify ``Loop.call_{at,later,soon}`` and ``Future.add_done_callback()`` to accept the new optional *context* keyword-only argument, which defaults to the current context:: def call_soon(self, callback, *args, context=None): if context is None: context = contextvars.copy_context() # ... some time later context.run(callback, *args) Tasks in asyncio need to maintain their own context that they inherit from the point they were created at. ``asyncio.Task`` is modified as follows:: class Task: def __init__(self, coro): ... # Get the current context snapshot. self._context = contextvars.copy_context() self._loop.call_soon(self._step, context=self._context) def _step(self, exc=None): ... # Every advance of the wrapped coroutine is done in # the task's context. self._loop.call_soon(self._step, context=self._context) ... Implementation ============== This section explains high-level implementation details in pseudo-code. Some optimizations are omitted to keep this section short and clear. The ``Context`` mapping is implemented using an immutable dictionary. This allows for a O(1) implementation of the ``copy_context()`` function. The reference implementation implements the immutable dictionary using Hash Array Mapped Tries (HAMT); see :pep:`550` for analysis of HAMT performance [1]_. For the purposes of this section, we implement an immutable dictionary using a copy-on-write approach and built-in dict type:: class _ContextData: def __init__(self): self._mapping = dict() def get(self, key): return self._mapping[key] def set(self, key, value): copy = _ContextData() copy._mapping = self._mapping.copy() copy._mapping[key] = value return copy def delete(self, key): copy = _ContextData() copy._mapping = self._mapping.copy() del copy._mapping[key] return copy Every OS thread has a reference to the current ``Context`` object:: class PyThreadState: context: Context ``contextvars.Context`` is a wrapper around ``_ContextData``:: class Context(collections.abc.Mapping): _data: _ContextData _prev_context: Optional[Context] def __init__(self): self._data = _ContextData() self._prev_context = None def run(self, callable, *args, **kwargs): if self._prev_context is not None: raise RuntimeError( f'cannot enter context: {self} is already entered') ts: PyThreadState = PyThreadState_Get() if ts.context is None: ts.context = Context() self._prev_context = ts.context try: ts.context = self return callable(*args, **kwargs) finally: ts.context = self._prev_context self._prev_context = None def copy(self): new = Context() new._data = self._data return new # Mapping API methods are implemented by delegating # `get()` and other Mapping methods to `self._data`. ``contextvars.copy_context()`` is implemented as follows:: def copy_context(): ts: PyThreadState = PyThreadState_Get() if ts.context is None: ts.context = Context() return ts.context.copy() ``contextvars.ContextVar`` interacts with ``PyThreadState.context`` directly:: class ContextVar: def __init__(self, name, *, default=_NO_DEFAULT): self._name = name self._default = default @property def name(self): return self._name def get(self, default=_NO_DEFAULT): ts: PyThreadState = PyThreadState_Get() if ts.context is not None: try: return ts.context[self] except KeyError: pass if default is not _NO_DEFAULT: return default if self._default is not _NO_DEFAULT: return self._default raise LookupError def set(self, value): ts: PyThreadState = PyThreadState_Get() if ts.context is None: ts.context = Context() data: _ContextData = ts.context._data try: old_value = data.get(self) except KeyError: old_value = Token.MISSING updated_data = data.set(self, value) ts.context._data = updated_data return Token(ts.context, self, old_value) def reset(self, token): if token._var is not self: raise ValueError( "Token was created by a different ContextVar") ts: PyThreadState = PyThreadState_Get() if token._ctx is not ts.context: raise ValueError( "Token was created in a different Context") if token._used: return if token._old_value is Token.MISSING: ts.context._data = data.delete(token._var) else: ts.context._data = data.set(token._var, token._old_value) token._used = True Note that the in the reference implementation, ``ContextVar.get()`` has an internal cache for the most recent value, which allows to bypass a hash lookup. This is similar to the optimization the ``decimal`` module implements to retrieve its context from ``PyThreadState_GetDict()``. See :pep:`550` which explains the implementation of the cache in great detail. The ``Token`` class is implemented as follows:: class Token: MISSING = object() def __init__(self, ctx, var, old_value): self._ctx = ctx self._var = var self._old_value = old_value self._used = False @property def var(self): return self._var @property def old_value(self): return self._old_value Summary of the New APIs ======================= Python API ---------- 1. A new ``contextvars`` module with ``ContextVar``, ``Context``, and ``Token`` classes, and a ``copy_context()`` function. 2. ``asyncio.Loop.call_at()``, ``asyncio.Loop.call_later()``, ``asyncio.Loop.call_soon()``, and ``asyncio.Future.add_done_callback()`` run callback functions in the context they were called in. A new *context* keyword-only parameter can be used to specify a custom context. 3. ``asyncio.Task`` is modified internally to maintain its own context. C API ----- 1. ``PyContextVar * PyContextVar_New(char *name, PyObject *default)``: create a ``ContextVar`` object. The *default* argument can be ``NULL``, which means that the variable has no default value. 2. ``int PyContextVar_Get(PyContextVar *, PyObject *default_value, PyObject **value)``: return ``-1`` if an error occurs during the lookup, ``0`` otherwise. If a value for the context variable is found, it will be set to the ``value`` pointer. Otherwise, ``value`` will be set to ``default_value`` when it is not ``NULL``. If ``default_value`` is ``NULL``, ``value`` will be set to the default value of the variable, which can be ``NULL`` too. ``value`` is always a new reference. 3. ``PyContextToken * PyContextVar_Set(PyContextVar *, PyObject *)``: set the value of the variable in the current context. 4. ``PyContextVar_Reset(PyContextVar *, PyContextToken *)``: reset the value of the context variable. 5. ``PyContext * PyContext_New()``: create a new empty context. 6. ``PyContext * PyContext_Copy()``: get a copy of the current context. 7. ``int PyContext_Enter(PyContext *)`` and ``int PyContext_Exit(PyContext *)`` allow to set and restore the context for the current OS thread. It is required to always restore the previous context:: PyContext *old_ctx = PyContext_Copy(); if (old_ctx == NULL) goto error; if (PyContext_Enter(new_ctx)) goto error; // run some code if (PyContext_Exit(old_ctx)) goto error; Design Considerations ===================== Why contextvars.Token and not ContextVar.unset()? ------------------------------------------------- The Token API allows to get around having a ``ContextVar.unset()`` method, which is incompatible with chained contexts design of :pep:`550`. Future compatibility with :pep:`550` is desired (at least for Python 3.7) in case there is demand to support context variables in generators and asynchronous generators. The Token API also offers better usability: the user does not have to special-case absence of a value. Compare:: token = cv.get() try: cv.set(blah) # code finally: cv.reset(token) with:: _deleted = object() old = cv.get(default=_deleted) try: cv.set(blah) # code finally: if old is _deleted: cv.unset() else: cv.set(old) Rejected Ideas ============== Replication of threading.local() interface ------------------------------------------ Please refer to :pep:`550` where this topic is covered in detail: [2]_. Backwards Compatibility ======================= This proposal preserves 100% backwards compatibility. Libraries that use ``threading.local()`` to store context-related values, currently work correctly only for synchronous code. Switching them to use the proposed API will keep their behavior for synchronous code unmodified, but will automatically enable support for asynchronous code. Examples ======== Converting code that uses threading.local() ------------------------------------------- A typical code that uses ``threading.local()`` usually looks like the following snippet:: class mylocal(threading.local): # Subclass threading.local to specify a default value. value = 'spam' mylocal = mylocal() # To set a new value: mylocal.value = 'new value' # To read the current value: mylocal.value Such code can be converted to use the ``contextvars`` module:: mylocal = contextvars.ContextVar('mylocal', 'spam') # To set a new value: mylocal.set('new value') # To read the current value: mylocal.get() Offloading execution to other threads ------------------------------------- It is possible to run code in a separate OS thread using a copy of the current thread context:: executor = ThreadPoolExecutor() current_context = contextvars.copy_context() executor.submit( lambda: current_context.run(some_function)) Reference Implementation ======================== The reference implementation can be found here: [3]_. References ========== .. [1] https://www.python.org/dev/peps/pep-0550/#appendix-hamt-performance-analysis .. [2] https://www.python.org/dev/peps/pep-0550/#replication-of-threading-local-interface .. [3] https://github.com/python/cpython/pull/5027 Copyright ========= This document has been placed in the public domain. .. Local Variables: mode: indented-text indent-tabs-mode: nil sentence-end-double-space: t fill-column: 70 coding: utf-8 End: _______________________________________________ Python-Dev mailing list Python-Dev@python.org https://mail.python.org/mailman/listinfo/python-dev Unsubscribe: https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com