您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

python – django 1.5中的django.utils.thread_support

5b51 2022/1/14 8:20:28 python 字数 2150 阅读 484 来源 www.jb51.cc/python

我正在尝试实现一个 django自定义中间件,它允许我访问请求对象,无论我在我的项目中的哪个位于 the one suggested here.那篇文章是很久以前写的, django 1.5没有库thread_support它回来了然后.我应该使用什么替代方法来完成一个线程安全的本地存储来存储请求对象?这是自定义中间件中的代码: from django.utils.thread_support i

概述

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

当然,它引发了一个例外:

ImproperlyConfigured: Error importing middleware myProject.middleware.global: 
"No module named thread_support"

编辑:

我发现了一个有效的方法

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

现在我有一个问题:是否会导致内存泄漏? _active会发生什么?当请求死亡时它被清理了吗?无论如何,已经发布了一个有效的答案.我将接受它,但任何其他(更好的,如果可能的话)解决方案将非常受欢迎!谢谢!

from django.utils.thread_support import currentThread
currentThread()

from threading import current_thread
current_thread()

总结

以上是编程之家为你收集整理的python – django 1.5中的django.utils.thread_support全部内容,希望文章能够帮你解决python – django 1.5中的django.utils.thread_support所遇到的程序开发问题。


如果您也喜欢它,动动您的小指点个赞吧

除非注明,文章均由 laddyq.com 整理发布,欢迎转载。

转载请注明:
链接:http://laddyq.com
来源:laddyq.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


联系我
置顶