为什么调度方法需要很长时间才能在Django Rest Framework中调用目标方法?

时间:2017-09-27 12:30:12

标签: django django-rest-framework django-views

我有这个方法

class RunningQuizAuthenticationView(APIView):

    def dispatch(self, *args, **kwargs):
        print(time.time())
        return super(RunningQuizAuthenticationView, self).dispatch(*args, **kwargs)

    def get(self, request: Request, key: str) -> Response:
        print(time.time())
        .....
        ......

现在,一旦我得到了这两个时间,我就取了这个时间的差异,差异时间大约是30毫秒,你能解释一下为什么这需要花费这么多时间,因为登录请求需要大约0毫秒但是对于其他请求从调度方法到达目标方法需要很长时间,请帮帮我,谢谢

1 个答案:

答案 0 :(得分:0)

我遇到了瓶颈,它是APIView的内部调度方法,它调用" perform_authentication"花费约30ms的方法,

def perform_authentication(self, request):
        """
        Perform authentication on the incoming request.

Note that if you override this and simply 'pass', then authentication will instead be performed lazily, the first time either `request.user` or `request.auth` is accessed. """ request.user