If I were you I would consider using Celery a powerful asynchronous task queue for Python. It allows you to run functions in the background without blocking your main web request or causing the page to hang.
Here’s a basic approach (You can find more in the docs)
Install Celery and a message broker like Redis
Move your response-generating loop into a Celery task
Trigger the task from your Django view without waiting for it to complete.