0

I'm using flask-restx to make an API, which relies on an sklearn-classifier which takes some time to load, but subsequently computes values from inputs very quickly. Naively, one might do something like the code below.

from flask import Flask
from flask_restx import Api, Resource
import time

app = Flask(__name__)
api = Api(app = app)

name_space = api.namespace('main', description='Main APIs')

class Foo():
    def __init__(self):
        # Simulate object that takes a while to create
        time.sleep(10)
    def compute(self, x):
        return 2*x


@name_space.route("/<int:x>")
class MainClass(Resource):
    def get(self, x):
        foo = Foo()
        status = foo.compute(x)
        return {
            "status": status
        }


if __name__ == '__main__':
    app.run()

However, this adds a very large overhead to each API call, as the 'Foo' object is instantiated in every call. What is the best practice (if one exists) for persisting the 'Foo' object between calls to avoid this overhead?

Note that the object in question is not distinct for each user or session, as in this question. Rather the Foo object is the same for every call to the API.

bjarkemoensted
  • 2,177
  • 2
  • 17
  • 35

0 Answers0