I'm looking to make some Tornado request data available globally for logging context. There have been a few previous discussions on this topic, and it looks like a StackContext is the solution:
- https://stackoverflow.com/questions/16572648/accessing-web-request-globally-in-tornado
- https://groups.google.com/forum/#!searchin/python-tornado/stackcontext$20global
For posterity's sake, can we document the right way to go about this? Here's my attempt, based on reading the previous topics: https://gist.github.com/simon-weber/7755289. Can somebody please look this over?
For what it's worth, I'm well aware that keeping this data global isn't great practice, but I think logging context is a reasonable motivation.
--
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/groups/opt_out.
That's how I had originally wrote it, but property() doesn't work out of the box with class methods. As for why I chose the metaclass solution instead of rewriting property(), I just think the former makes more sense (by following the instance < class < metaclass hierarchy).
Anywho, good to hear I got it mostly correct! Thanks for looking it over.I've just got a few quick things I'm specifically concerned about:
- my understanding of StackContexts is that they're a way to hook entering/exiting Tornado "context switches". Based on this, is there any way to "re-enter" a Context? i.e. are __enter__/__exit__ guaranteed to be called one time each?
- will this work with async Handlers?
- will the Contexts be free for garbage collection after the request is finished?
- is there any sane way to test this? I was thinking I'd have to make my own IOLoop implementation to force ordering of events and verify the global data while single-stepping through them.