Skip to content
Advertisement

Python: Run a code after return something

This is an ApiView from my django project:

class Receive_Payment(APIView):
    authentication_classes = (TokenAuthentication,)
    permission_classes = (IsAdminUser,)
    def get(self, request, cc_number):
        if Card.objects.filter(cc_num = cc_number).exists():
            client = Client.objects.get(client_id = (Card.objects.get(cc_num = cc_number).client))
            if not client.is_busy:
                client.is_busy = True
                client.save()
                resp = "successful"
            else:
                resp = "client_is_busy"
        else:
            resp = "fail"
        return Response({"response": resp})

As you see, if client.is_busy is not True, I’m making it True in here. But in this situation, I need to client.is_busy = False after 30 seconds. If I do it under client.save() code, it delays to response. How can I do it?

Advertisement

Answer

You should consider using timer objects here.

e.g.

import threading
def free_up_client(client):
   client.is_busy = false

timer = Theading.timer(30.0, free_up_client, [client])
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement