I understand this may not be possible. Let's say I have a function get_data(). This function takes 10-20 seconds to run and it retrieves a bunch of data from a database and processes it. It processes through a database with hundreds of thousands of rows and returns a few thousand rows in response. This few thousand rows is quite large—probably a few megabytes of JSON, more than I'd rather load in an API response. So, I would like to be able to paginate my response. However, the issue I'm running into is that every time you go to the next page, it reruns the query, which means that every single response takes a long time. Here is the code I'm using
class Query(APIView):
def get(self, request, format=None):
paginator = PageNumberPagination()
paginator.page_size = 10
data = get_data()
page = paginator.paginate_queryset(data, request, view=self)
return paginator.get_paginated_response(page)
I almost wonder if the best way to deal with this is to open a websocket between client and API and pass the data as it's requested? Or perhaps have a system of caching where each time the client makes a request, they generate some unique token (probably would be made by caching all query parameters as well as the exact timestamp at which the request was made) which attaches to their request, and then any time they ask for the next page of data, that unique token is attached to the request and the cached response is taken. Not sure if that would be better than a websocket. Any help is greatly appreciated.