[Python-Dev] PEP 567 v3 (original) (raw)

Yury Selivanov yselivanov.ml at gmail.com
Wed Jan 17 20:53:42 EST 2018


On Wed, Jan 17, 2018 at 2:24 PM, Guido van Rossum <gvanrossum at gmail.com> wrote:

Perhaps you can update the PEP with a summary of the rejected ideas from this thread?

The Rejected Ideas section of the PEP is now updated with the below:

Token.reset() instead of ContextVar.reset()

Nathaniel Smith suggested to implement the ContextVar.reset() method directly on the Token class, so instead of::

token = var.set(value)
# ...
var.reset(token)

we would write::

token = var.set(value)
# ...
token.reset()

Having Token.reset() would make it impossible for a user to attempt to reset a variable with a token object created by another variable.

This proposal was rejected for the reason of ContextVar.reset() being clearer to the human reader of the code which variable is being reset.

Make Context objects picklable

Proposed by Antoine Pitrou, this could enable transparent cross-process use of Context objects, so the Offloading execution to other threads_ example would work with a ProcessPoolExecutor too.

Enabling this is problematic because of the following reasons:

  1. ContextVar objects do not have __module__ and __qualname__ attributes, making straightforward pickling of Context objects impossible. This is solvable by modifying the API to either auto detect the module where a context variable is defined, or by adding a new keyword-only "module" parameter to ContextVar constructor.

  2. Not all context variables refer to picklable objects. Making a ContextVar picklable must be an opt-in.

Given the time frame of the Python 3.7 release schedule it was decided to defer this proposal to Python 3.8.

Yury



More information about the Python-Dev mailing list