Djangocuser、请求处理和线程

2024-04-19 04:00:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用Apache mod\u wsgi为django服务。I'v还使用Django-cuser中间件,使用户信息始终可用。我不能真正理解这个中间件是如何工作的。具体源代码:

class CuserMiddleware(object):
    @classmethod
    def get_user(cls, default=None):
        """
        Retrieve user info
        """
        return cls.__users.get(threading.current_thread(), default)

    @classmethod
    def set_user(cls, user):
        """
        Store user info
        """
        if isinstance(user, str):
            user = User.objects.get(username=user)
        cls.__users[threading.current_thread()] = user

为什么使用线程。当前线程()作为用户的密钥?如我所知,Apache不会为请求创建不同的thead。你知道吗


Tags: 中间件用户infodefaultgetapachedefcurrent