Discussion:
[tornado] ContextVars and Tornado
Michael Robellard
2018-08-16 02:28:10 UTC
Permalink
I have successfully moved one of my Tornado based projects over to Python
3.7 and was wondering if there were any thoughts or ideas of including
contextvars in Tornado itself.

From looking at the documentation for contextvars, It appears that I can
use it even without built-in support for it in Tornado, but I was wondering
if anyone had any plans for it.

Thanks,

Mike
--
You received this message because you are subscribed to the Google Groups "Tornado Web Server" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python-tornado+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Michael Robellard
2018-08-16 02:39:03 UTC
Permalink
So I was just looking through the contextvars documentation in Python 3.7
again and was reading this snippet about asyncio support:18.3. asyncio
support <https://docs.python.org/3/library/contextvars.html#asyncio-support>

Context variables are natively supported in asyncio
<https://docs.python.org/3/library/asyncio.html#module-asyncio> and are
ready to be used without any extra configuration. For example, here is a
simple echo server, that uses a context variable to make the address of a
remote client available in the Task that handles that client:


From my understanding, as of Tornado 5.0 asyncio is used automatically when
available (python 3.5+), so that should mean that contextvars are properly
handled (context switched automatically) correct?
Post by Michael Robellard
I have successfully moved one of my Tornado based projects over to Python
3.7 and was wondering if there were any thoughts or ideas of including
contextvars in Tornado itself.
From looking at the documentation for contextvars, It appears that I can
use it even without built-in support for it in Tornado, but I was wondering
if anyone had any plans for it.
Thanks,
Mike
--
You received this message because you are subscribed to the Google Groups "Tornado Web Server" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python-tornado+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Ben Darnell
2018-08-16 03:18:06 UTC
Permalink
Post by Michael Robellard
From my understanding, as of Tornado 5.0 asyncio is used automatically
when available (python 3.5+), so that should mean that contextvars are
properly handled (context switched automatically) correct?
I haven't really looked into this yet but there may still be some work to
be done. It will work automatically for native (`async def`) coroutines,
but what happens if you call a Tornado `@gen.coroutine`? I think the
context might be lost there.

-Ben
Post by Michael Robellard
Post by Michael Robellard
I have successfully moved one of my Tornado based projects over to Python
3.7 and was wondering if there were any thoughts or ideas of including
contextvars in Tornado itself.
From looking at the documentation for contextvars, It appears that I can
use it even without built-in support for it in Tornado, but I was wondering
if anyone had any plans for it.
Thanks,
Mike
--
You received this message because you are subscribed to the Google Groups
"Tornado Web Server" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Tornado Web Server" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python-tornado+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Michael Robellard
2018-08-26 22:10:26 UTC
Permalink
So I ran some tests today on Python 3.7 with the latest version of Tornado.
I was only using native async coroutines, but it seems to work. I just was
hoping someone else could look at my testing method and results and see if
they agree that I properly tested it.

I have included the files for the test.

The testrunner just runs six fetches asynchronously to the tornado web
server.

test_contextvars creates a simple tornado web server that expects an
integer query parameter. It saves the query param in a context var. It then
async sleeps for 0-3 seconds before calling a function that loads the
contextvar that was saved earlier. For comparison sake, I compare it to a
parameter passed into the function. This is a very contrived example and
not how you would actually use contextvars. I just wanted to make sure they
worked properly in Tornado before I tried to use them.
Post by Ben Darnell
Post by Michael Robellard
From my understanding, as of Tornado 5.0 asyncio is used automatically
when available (python 3.5+), so that should mean that contextvars are
properly handled (context switched automatically) correct?
I haven't really looked into this yet but there may still be some work to
be done. It will work automatically for native (`async def`) coroutines,
context might be lost there.
-Ben
Post by Michael Robellard
Post by Michael Robellard
I have successfully moved one of my Tornado based projects over to
Python 3.7 and was wondering if there were any thoughts or ideas of
including contextvars in Tornado itself.
From looking at the documentation for contextvars, It appears that I can
use it even without built-in support for it in Tornado, but I was wondering
if anyone had any plans for it.
Thanks,
Mike
--
You received this message because you are subscribed to the Google Groups
"Tornado Web Server" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Tornado Web Server" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python-tornado+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...