Making the HTTP object available globally across the app

1
2
3
4
5
6
"""
Snippet removed - this approach is *NOT* thread safe.
Snippet removed - this approach is *NOT* thread safe.
Snippet removed - this approach is *NOT* thread safe.
Snippet removed - this approach is *NOT* thread safe.
"""

More like this

  1. GlobalRequest middleware by myq 5 months, 3 weeks ago
  2. Improved Pickled Object Field by taavi223 3 years, 9 months ago
  3. Effective content caching for mass-load site using redirect feature by nnseva 1 year, 10 months ago
  4. RequiredNullBooleanField by wwu.housing 4 years, 1 month ago
  5. Add get_addr() method to request object by nikmolnar 5 months, 1 week ago

Comments

sjaensch (on February 7, 2011):

This is not only slightly frowned upon, it might very well not work the way you expect it to. If your web server executes multiple threads per python process, threads will overwrite eachother's request object since global variables are shared among threads. If you really want to do this use thread-locals. But that's a bad idea as well: http://code.djangoproject.com/wiki/CookBookThreadlocalsAndUser

Generally, if you need the request (or user) object somewhere, pass it as a parameter. If that's not possible then you are either doing something wrong or missing the solution to your problem. Most likely, thread-locals aren't the best one.

#

(Forgotten your password?)