Home > Net >  How to limit execution of python flask function to single instance
How to limit execution of python flask function to single instance

Time:12-02

I have a Python Flask page which is extremely slow to generate. It takes about 1 minute to pull all the data from external APIs, process the data before returning the page. Fortunately, the data is valid for up to 1 hour so I can cache the result and return cached results quickly for most of the requests.

This works well except for the minute after the cache expires. If 10 requests were made within that single minute, there will be 10 calls to veryslowpage() function, this eats up the HTTPS connection pool due to the external API calls and eats up memory due to the data processing, affecting other pages on the site. Is there a method to limit this function to a single instance, so 10 requests will result in only 1 call to veryslowpage() while the rest wait until the cached result is ready?

from flask import Flask, request, abort, render_template
from flask_caching import Cache

@app.route('/veryslowpage', methods=['GET'])
@cache.cached(timeout=3600, query_string=True)
def veryslowpage():
  data = callexternalAPIs()
  result = heavydataprocessing(data)
  return render_template("./index.html", content=result)

CodePudding user response:

You could simply create a function that periodically fetch the data from API (every hour) and store it in your database. Then in your route function read the data from your database instead of external API.

A better approach is creating a very simple script and call it (in another thread) in your app/init.py that fetch the data every one hour and update the database.

CodePudding user response:

You could create a file or a database entry that contains the information that you are calculating the response in a different thread. Then, your method would check if such a file exists and if it does, let it wait and check for the response periodically.

You could also proactively create the data once every hour (or every 59 minutes if that matters) so you always have a fresh response available. You could use something like APScheduler for this.

  • Related