This might be a python question. It is a noobish one to be sure.
A client requests a calculation-intensive page [page-1] and will ultimately request a second calculation-intensive page [page-2], which can be calculated the instance the request for page-1 is known. I don’t want to calculate each set of data before serving page-1 because it will significantly slow down the performance of the initial response.
I do want to calculate the value for page-2 while the client reads page-1. The client also might click on some buttons which cause a response that provides a different view of page-1 data, but don’t necessitate an intensive calculating. Eventually but not necessarily immediately, the client will ask for page-2 and I want to be able to response with a pre-rendered response.
How do I do this?
As mentioned in the comments, it sounds like you’re going to need to handle this with an asynchronous background task, saving the result in the Django low level cache. I would personally use celery for the task queue.
Basically, after page one is requested, you would add an asynchronous task to start the page 2 calculations, storing the result in the cache. So, when page 2 is requested, you check for the pre-rendered response in the cache, and if it doesn’t exist, you could calculate the value synchronously.
So, your code would look something like this (the task would be in a task.py file in your app, but this should give you a general idea):