0

My react front end uses an abortController to signal when the useFetch hook is unmounted (if the url changes, if the page is refreshed etc). While the data is no longer being received on the front end, I believe that the request is still being processed by the flask backend (spun up by gunicorn).

My question is - is there an elegant or common way to cancel the flask request? Some of my requests deal with large chunks of data being queried from the database (resulting in tens of seconds of wait time) and I would like those to cancel instead of run through and steal API time and memory.

I have seen this stack overflow: Stop processing Flask route if request aborted but it was 9 years ago. I also read through Flask: What happens when a user closes the browser while a long process is being executed? - I was wondering if there is a better way to approach the problem 10 years later since this most recent post from 2021 references the older post.

1 Answers1

1

There is no standard way to cancel a request in Flask, but there are a few ways you can achieve this. One approach would be to keep track of all in-progress requests in a global variable, and then have a route that can be called to cancel a request. Another approach would be to use a context manager to keep track of all in-progress requests, and then have a route that can be called to cancel all requests.

JDong
  • 2,304
  • 3
  • 24
  • 42