1

I am running SQLAlchemy with MySQL in combination with a Flask API. We use models for the requests and create and update changes with db.session.add() and db.session.commit().

We are also running the processing of the data in parallel - its an infinite while loop which queries the database on every run (Model.query.filter_by(...).all()) - however, we only get the data that was in the database before and not the updated data - the data itself is already in the database. If we restart the application, the new data is found.

The DB is created with db = SQLAlchemy(app) and then imported and used in the models

class Model(db.Model):
    name = db.Column(db.String(255), index=True, unique=True)
    active = db.Column(db.Boolean)

    def create(self):
        db.session.add(self)
        db.session.commit()

    def update(self):
        db.session.commit()


def run_update():
    while True:

        models = Model.query.filter_by(active=True).all()
        for model in models:
            do_something(model)
            time.sleep(300)

def do_something():
    longtask = threading.Thread(target=long_task)
    longtask.start()


def long_task():
    // Update database here again
    // Task has finished before it is run again.
    time.sleep(10)

If a new entry is created, the result is only captured in the model query after restarting the app.

I already tried to replace the Model.query queries with db.session.query(Model) but that did not change the result.

Thanks in advance,

Mou
  • 113
  • 2
  • 14

1 Answers1

2

I added an db.session.close() at the end of the while loop - now the data is reloaded.

Mou
  • 113
  • 2
  • 14