On 27/08/10 14:22, Sells, Fred wrote: > I'm not sure of the thread safety of Django and wonder if I could store > this object as a local variable of some module like
No, that is not likely to work except in a single-threaded* context, and even then it's a bit fraught (just being single-threaded still doesn't mean each request is handled by a _new_ process, if you're not careful you could leave things set to values from old requests). There is a recipe out there for using a django middleware that uses thread-local variables to stash info from the django request [1], but that also tends to be frowned upon by django people for a variety of reasons [2]. Python, while obviously somewhat lisp-oid, also doesn't have lisp-like dynamics that might be idiomatic in lisp land. Sooo.... just give up and explicitly pass the datum you want down through the call chain. That can be a pain, but at least python has the *args/**kwargs mechanism that can make it fairly easy for one callable to just pass on through unused kwargs to another (a pattern used quite a bit in django itself). (* aside: single-threaded multiple-process serving is often a viable option for serving stuff, at least on linux, where processes are very cheap, and does avoid threading issues nicely (remember ordinary python 2.x can be kinda sucky threading-wise anyway [3]). Ex-windows people on linux/unix often jump to threading very prematurely, mistakenly generalising from windows [4]) [1] http://djangosnippets.org/snippets/1605/ [2] http://code.djangoproject.com/wiki/CookBookThreadlocalsAndUser [3] http://en.wikipedia.org/wiki/Global_Interpreter_Lock [4] http://stackoverflow.com/questions/47845/why-is-creating-a-new-process-more-expensive-on-windows-than-linux -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.