简体   繁体   中英

Updating loaded data in flask restful api app

I'm looking for the best option to reload data in a deployed app. Best is defined as, must not result in 500s and must update the data (not fail silently), should not block working for too long, but no 500s and update is the priority.

The app is a CPU limited app, which I'm scaling by adding more workers and cores. The app loads a list or dict at the on start up via

api.add_resource(
Cars,
'/cars',
resource_class_kwargs={'carslist': carslist})

The problem is that every 24h or on occasion more often the carslist changes. I can make a POST method with which I send a new one to the app, or make it load a new version etc. My question is though, if I deploy this with gunicorn and say 8 workers, how can I make sure each worker has the up-to-date carslist .

According to the gunicorn docs I can also gracefully restart gunicorn with HUP . So is restarting the app and make it load a new carslist the best option, or how can I make a request that every worker is called by?

I'm using gunicorn behind nginx on docker, and this app is not facing the internet, so security is not a concern, but I do see 10000s requests in a second on occasion.

There are different approaches to your task and it depends on what your constrains on how fast workers need to catch up to new values, how you announce them when this value changes and what is your performance requirements. What those solutions share, is that it's easier to control carlist when it's a class object with interface like:

class CarlistManager:
    def __init__(self):
        super().__init__()
        self.carlist = self.update()

    def update(self):
        self.carlist = ...  # depends on where you store a carlist

    def get(self):
        if not self.is_up_to_date():
            self.update()
        return self.carlist

    def is_up_to_date(self):
        # depends on how do you prefer to check for a new file version

app = Flask("app")
app.carlist_manager = CarlistManager()
api.add_resource(Cars, '/cars',
resource_class_kwargs={'carslist': app.carlist_manager})

If you have a DB, you can store there some value, like a filename of a newest carlist, update it on a new posted file and check if it's changed in is_up_to_date. This allows you to change a value in a runtime, without restarting any worker.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM