[Python-Dev] PEP 550 v4

Elvis Pranskevichus elprans at gmail.com
Thu Sep 7 10:17:15 EDT 2017


On Thursday, September 7, 2017 10:06:14 AM EDT Ethan Furman wrote:
> I might be, and I wouldn't be surprised.  :)  On the other hand, one
> can look at isolation as being a resource.
> > threading.local(), the isolation mechanism, is *implicit*.
> 
> I don't think so.  You don't get threading.local() unless you call it
> -- that makes it explicit.
> > decimal.localcontext() is an  *explicit* resource manager that
> > relies on threading.local() magic.  PEP 550 simply provides a
> > threading.local() alternative that works in tasks and generators. 
> > That's it!
> 
> The concern is *how* PEP 550 provides it:
> 
> - explicitly, like threading.local(): has to be set up manually,
>    preferably with a context manager
> 
> - implicitly: it just happens under certain conditions
> 

You literally replace threading.local() with contextvars.ContextVar():

import threading

_decimal_context = threading.local()

def set_decimal_context(ctx):
    _decimal_context.context = ctx


Becomes:


import contextvars

_decimal_context = contextvars.ContextVar('decimal.Context')

def set_decimal_context(ctx):
    _decimal_context.set(ctx)


                               Elvis


More information about the Python-Dev mailing list