2

A django project I work on allows a user to preview a document made on our site before downloading it. The process takes some time on the backend which is why we allow the user to cancel the operation on the frontend. But even after the user cancels the process, it still keeps running on the server wasting time and compute. We use a simple django-rest-framework api function with no socket connection or anything asynchronous that allows the frontend to track the progress of the task.

I would like to know if there is some way we can abort the function execution on the backend if the user decides to cancel the operation on the frontend.

I'd share any code but I don't think it'll be useful because the function just prepares the document and then sends it.

  • Here's another relevant SO question: https://stackoverflow.com/questions/39780755/propagate-http-abort-close-from-nginx-to-uwsgi-django But no, I don't believe there is any easy way to do this without some re-architecting to an async task and a client implementation that polls for the async task to complete and can send an api call to cancel the job. – azundo May 03 '21 at 07:25
  • @azundo thanks for the prompt response. Assuming we could send an api call to cancel the job, how would the backend go about it? I've heard about celery but I don't know if it provides these options out-of-the-box – Raspreet Singh May 03 '21 at 08:11
  • Yep, celery has the ability to revoke and terminate running tasks: https://stackoverflow.com/a/8924116/11847125 RQ (another async task queue that is a bit simpler to operate in my opinion) also has the ability to stop jobs: https://python-rq.org/docs/jobs/#stopping-a-currently-executing-job – azundo May 03 '21 at 17:22

0 Answers0