简体   繁体   中英

Waiting for another request in Django view

The title is pretty much self explaining, I need for one of my views to wait for another view to be called or timeout. It would look something like this:

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)

I already have tried to use the Event object of the threading module, making use of its wait() and set() methods, but either I'm doing it wrong either it's just not the way to go for this use case. More about that attempt here .

You can use session variable to share data between views:

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")'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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