PEP-550: Mention why threading.local()-like interface was rejected (#377)
This commit is contained in:
parent
977886f683
commit
08871010d4
55
pep-0550.rst
55
pep-0550.rst
|
@ -1195,6 +1195,58 @@ Backwards Compatibility
|
|||
This proposal preserves 100% backwards compatibility.
|
||||
|
||||
|
||||
Rejected Ideas
|
||||
==============
|
||||
|
||||
Replication of threading.local() interface
|
||||
------------------------------------------
|
||||
|
||||
Choosing the ``threading.local()``-like interface for context
|
||||
variables was considered and rejected for the following reasons:
|
||||
|
||||
* A survery of the standard library and Django has shown that the
|
||||
vast majority of ``threading.local()`` uses involve a single
|
||||
attribute, which indicates that the namespace approach is not
|
||||
as helpful in the field.
|
||||
|
||||
* Using ``__getattr__()`` instead of ``.get()`` for value lookup
|
||||
does not provide any way to specify the depth of the lookup
|
||||
(i.e. search only the top logical context).
|
||||
|
||||
* Single-value ``ContextVar`` is easier to reason about in terms
|
||||
of visibility. Suppose ``new_context_var()`` returns a namespace,
|
||||
and the consider the following::
|
||||
|
||||
ns = new_context_var('ns')
|
||||
|
||||
def gen():
|
||||
ns.a = 2
|
||||
yield
|
||||
assert ns.b == 'bar' # ??
|
||||
|
||||
def main():
|
||||
ns.a = 1
|
||||
ns.b = 'foo'
|
||||
g = gen()
|
||||
next(g)
|
||||
# should not see the ns.a modification in gen()
|
||||
assert ns.a == 1
|
||||
# but should gen() see the ns.b modification made here?
|
||||
ns.b = 'bar'
|
||||
yield
|
||||
|
||||
The above example demonstrates that reasoning about the visibility
|
||||
of different attributes of the same context var is not trivial.
|
||||
|
||||
* Single-value ``ContextVar`` allows straightforward implementation
|
||||
of the lookup cache;
|
||||
|
||||
* Single-value ``ContextVar`` interface allows the C-API to be
|
||||
simple and essentially the same as the Python API.
|
||||
|
||||
See also the mailing list discussion: [26]_, [27]_.
|
||||
|
||||
|
||||
Appendix: HAMT Performance Analysis
|
||||
===================================
|
||||
|
||||
|
@ -1340,6 +1392,9 @@ References
|
|||
|
||||
.. [25] https://mail.python.org/pipermail/python-ideas/2017-August/046786.html
|
||||
|
||||
.. [26] https://mail.python.org/pipermail/python-ideas/2017-August/046888.html
|
||||
|
||||
.. [27] https://mail.python.org/pipermail/python-ideas/2017-August/046889.html
|
||||
|
||||
Copyright
|
||||
=========
|
||||
|
|
Loading…
Reference in New Issue