繁体   English   中英

在 Django 视图中等待另一个请求

[英]Waiting for another request in Django view

标题几乎是自我解释的,我需要我的一个视图等待另一个视图被调用或超时。 它看起来像这样:

class WaitingView(APIView):
    def post(self, request):
        ...do_things
        called = wait_for_DeblockingView_or_timeout()
        if called:
            return Response(200)
        return Response(408)


class DeblockingView(APIView):
    def post(self, request):
        ...do_things
        send_some_signal_to_unlock()
        return Response(200)

我已经尝试使用threading模块的Event object,利用它的wait()set()方法,但要么我做错了要么它不是 go 对于这个用例的方法。 更多关于这里的尝试。

您可以使用session变量在视图之间共享数据:

class WaitingView(APIView):
    def post(self, request):
        ...do_things
        called = request.session.get('deblocking_call')
        if called is True:
            del request.session['deblocking_call']
            return Response(200)
        return Response(408)


class DeblockingView(APIView):
    def post(self, request):
        ...do_things
        request.session['deblocking_call'] = True

        return Response(200) ## I would use 'return redirect("waiting_view")'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM