0

my Python Flask-restful API exposes a GET endpoint, /get_value, which takes some time to run, and it's crucial that this operation occurs in the fastest time possible. Please consider that multiple requests may occur in sequence from a client application.

Now, I want to store on a DB a log record everytime the /get_value endpoint is called, without affecting the API response (writing on a DB is an overhead I can't have while producing the GET response!). The record insertion into the DB has a low priority, and could be done even one day after that the endpoint is called...

Which is the best practice for doing this? Does Flask support a function for that?

Thanks!

user1403546
  • 1,680
  • 4
  • 22
  • 43
  • The first thing that comes to my mind is Redis task queue, check out https://python-rq.org/ – vremes Dec 23 '20 at 09:12

1 Answers1

0

This is a complex problem and I think your question is linked to Execute a function after Flask returns response.

If you read this post and the answers you will find severall solutions to your problem. One of them is to use the decorator @response.call_on_close - see here for a different example - which seems to do what you want to do.

If you want to use multithreading, this answer should be helpful.

Both - the decorator and multithreading - have the advantage of avoiding additional dependencies.

HTH

MacOS
  • 1,149
  • 1
  • 7
  • 14