<div dir="ltr">Hey Nathaniel, can you bring this up in the PEP 492 thread? It sounds like it would be super great if the async with statement would (optionally?) notify the context manager of switching to a different task and back.<br></div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Apr 21, 2015 at 10:36 PM, Nathaniel Smith <span dir="ltr"><<a href="mailto:njs@pobox.com" target="_blank">njs@pobox.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">On Tue, Apr 21, 2015 at 4:48 PM, Eric Snow <<a href="mailto:ericsnowcurrently@gmail.com">ericsnowcurrently@gmail.com</a>> wrote:<br>
> threading.local provides thread-local namespaces. As far as I can<br>
> tell, there isn't an equivalent for coroutines (asyncio), even though<br>
> I would expect they would provide the same benefits. I'd like to see<br>
> coroutine-local namespaces added and would be happy to work on the<br>
> problem. I plan on working on a feature that will rely on applying a<br>
> thread-local context and realized that coroutines would need a similar<br>
> treatment. Also, there are probably a few spots in the stdlib that<br>
> would benefit (e.g. decimal contexts).<br>
<br>
</span>Possibly this is off-topic for this thread but -- it's a good point<br>
that, currently, when implementing a global state variable (like the<br>
decimal context), one can bind it to a single thread by storing the<br>
state in TLS, but AFAIK there's no way to do this with coroutines.<br>
It's somewhat unpleasant that<br>
<br>
with decimal.localcontext() as ctx:<br>
ctx.prec = 10<br>
values = (yield from db.fetchstuff())<br>
morecodehere()<br>
<br>
can cause arbitrary other code to run with the given decimal context<br>
in effect, while also failing to guarantee that morecodehere() will<br>
run with any particular decimal context.<br>
<br>
I hoped for a second that "async with" might help with this, but<br>
checking the PEP it doesn't -- to do that it would need support<br>
context manager callbacks that were called whenever the coroutine got<br>
suspended/resumed while the 'with' block was in effect, but that's not<br>
what it does, it just allows the __enter__ and __exit__ methods<br>
themselves to be coroutines.<br>
<br>
And AFAICT the thread Guido linked<br>
(<a href="https://github.com/python/asyncio/issues/165" target="_blank">https://github.com/python/asyncio/issues/165</a>) only considers cases<br>
where an async framework (e.g. a web server framework) is the one who<br>
wants to set global variables that can be accessed from within a set<br>
of coroutines. This works out, b/c when the same code controls both<br>
the spawning of coroutines (by creating a new Task) and setting the<br>
variables, so it can bind them together. But that doesn't seem to help<br>
for something like the decimal module context, or numpy's error<br>
handling context<br>
(<a href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.errstate.html" target="_blank">http://docs.scipy.org/doc/numpy/reference/generated/numpy.errstate.html</a>).<br>
<br>
Obviously one can just document that users should always bracket any<br>
yield/await calls with explicit state setting/restore functions, but<br>
is there any better solution?<br>
<span class="HOEnZb"><font color="#888888"><br>
-n<br>
<br>
--<br>
Nathaniel J. Smith -- <a href="http://vorpus.org" target="_blank">http://vorpus.org</a><br>
</font></span><div class="HOEnZb"><div class="h5">_______________________________________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org">Python-ideas@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-ideas" target="_blank">https://mail.python.org/mailman/listinfo/python-ideas</a><br>
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" target="_blank">http://python.org/psf/codeofconduct/</a><br>
</div></div></blockquote></div><br><br clear="all"><br>-- <br><div class="gmail_signature">--Guido van Rossum (<a href="http://python.org/~guido" target="_blank">python.org/~guido</a>)</div>
</div>