python - django.utils.thread_support in django 1.5 -
i'm trying implement django custom middleware gives me access request
object wherever in project, based in the 1 suggested here. article written long time ago, , django 1.5 not have library thread_support
then. alternative should use accomplish thread safe local store store request object? code in custom middleware:
from django.utils.thread_support import currentthread _requests = {} def get_request(): return _requests[currentthread()] class globalrequestmiddleware(object): def process_request(self, request): _requests[currentthread()] = request
and, of course, raises exception:
improperlyconfigured: error importing middleware myproject.middleware.global: "no module named thread_support"
edit:
i found out working fix:
from threading import local _active = local() def get_request(): return _active.request class globalrequestmiddleware(object): def process_view(self, request, view_func, view_args, view_kwargs): _active.request = request return none
now have question: leads memory leak? happens _active
? cleaned when request dies? anyway, there's valid answer posted. i'm going accept other (better, if possible) solution welcome! thanks!
replace
from django.utils.thread_support import currentthread currentthread()
with
from threading import current_thread current_thread()
Comments
Post a Comment