Django streaming response fake to avoid Heroku timeout

I have a Django application that uses django-wkhtmltopdf to create PDF files on Heroku. Some responses exceed a 30 second timeout. Since this is proof of the concept that works at the free level, I would rather not break what I need in order to proceed with the work / survey process. My current view is as follows:

 def dispatch(self, request, *args, **kwargs): do_custom_stuff() return super(MyViewClass, self).dispatch(request, *args, **kwargs) 

Is it possible to override the dispatch method of the view class to fake a streaming response like this or using the Empy Chunking approach mentioned here to send an empty response until the PDF is rendered? Sending an empty byte will restart the timeout process , giving a lot of time to send a PDF file.

+7
django pdf heroku
source share
1 answer

I solved a similar problem using Celery, something like this.

 def start_long_process_view(request, pk): task = do_long_processing_stuff.delay() return HttpResponse(f'{"task":"{task.id}"}') 

Then you can have a second view that can check the status of the task.

 from celery.result import AsyncResult def check_long_process(request, task_id): result = AsyncResult(task_id) return HttpResponse(f'{"state":"{result.state}"') 

Finally, using javascript, you can simply get the status right after the task starts. Updating every half second will be more than enough to give your users good feedback.

If you think there are a lot of Celery, there are light alternatives that will work just fine: https://djangopackages.org/grids/g/workers-queues-tasks/

+3
source share

All Articles