ContextVars and Tornado

359 views
Skip to first unread message

Michael Robellard

unread,
Aug 15, 2018, 10:28:10 PM8/15/18
to Tornado Web Server
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

Michael Robellard

unread,
Aug 15, 2018, 10:39:03 PM8/15/18
to Tornado Web Server
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

Context variables are natively supported in 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?

Ben Darnell

unread,
Aug 15, 2018, 11:18:21 PM8/15/18
to python-...@googlegroups.com
On Wed, Aug 15, 2018 at 10:39 PM Michael Robellard <mi...@noggingroup.com> wrote:
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 
 



On Wednesday, August 15, 2018 at 10:28:10 PM UTC-4, Michael Robellard wrote:
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-tornad...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Michael Robellard

unread,
Aug 26, 2018, 6:10:27 PM8/26/18
to Tornado Web Server
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.
test_contextvars.py
testrunner.py
Reply all
Reply to author
Forward
0 new messages