Search code examples
djangodjango-rest-frameworkdjango-viewsrequestresponse

Continue request django rest framework


I have a request that lasts more than 3 minutes, I want the request to be sent and immediately give the answer 200 and after the end of the work - give the result


Solution

  • The workflow you've described is called asynchronous task execution.

    The main idea is to remove time or resource consuming parts of work from the code that handles HTTP requests and deligate it to some kind of worker. The worker might be a diffrent thread or process or even a separate service that runs on a different server.

    This makes your application more responsive, as the users gets the HTTP response much quicker. Also, with this approach you can display such UI-friendly things as progress bars and status marks for the task, create retrial policies if task failes etc.

    Example workflow:

    1. user makes HTTP request initiating the task
    2. the server creates the task, adds it to the queue and returns the HTTP response with task_id immediately
    3. the front-end code starts ajax polling to get the results of the task passing task_id
    4. the server handles polling HTTP requests and gets status information for this task_id. It returns the info (whether results or "still waiting") with the HTTP response
    5. the front-end displays spinner if server returns "still waiting" or the results if they are ready

    The most popular way to do this in Django is using the celery disctributed task queue.