SQL Query not getting values after the same query is ran

Question:

so I have this class with a class session object that initializes a session in init: self.session = Session() and then I have a run method that makes a query processes some data and then returns, and all of this is managed by the schedule module so the run method runs every n seconds. My issue is the first time the function run is ran it gets the values from db but if I add a new entry and wait for the next loop when it queries the db no values are obtained by the query.

Some representative code:

class Transcriber(STT, Notifications):
    def __init__(self):
        super(Transcriber, self).__init__(model_name=MODEL)

        self.logger = get_logger('Transcriber')

        self.session = Session()

    def run(self):
        """
            Runs the program

            @return:
        """
        entry: TranscriptionQueue

        self.logger.info('Collecting new entries ...')

        awaiting_entries = self.session.query(TranscriptionQueue).filter_by(
            status='awaiting', language=self.language.upper()
        ).limit(5).all()

        for entry in awaiting_entries:
             entry.set_processing()
             self._process_entry(entry)


if __name__ == '__main__':
    clean_temp_folder()

    transcriber = Transcriber()

    schedule.every(LOOP_WAIT_TIME).seconds.do(transcriber.run)
    schedule.every(1).hours.do(clean_temp_folder)

    while True:
        schedule.run_pending()
        time.sleep(1)

Problem diagram:
Data added to DB -> Turn on program -> Runs run function and processes data -> More data added to DB -> Waits for next loop -> No data is obtained by query.

In case your wondering if the function run is running more then once yes it is as I have a print statement printing the variable awaiting_entries every time run in ran.

Does this have anything to do with Sessions keeping a cache of queries? Do I need to refresh the session? Should I initiate a new session every time run is ran?

Ideally keeping the session open would be better since this runs every n seconds so closing and reopening would be a waste of resourses.

Asked By: DeadSec

||

Answers:

The way sqlalchemy sessions work, they store locally a sort of cache that doesn’t get reloaded until you either run session.refresh(object) or close and reopen the session.

This was fixed by doing:

with Session() as session:
    <code>

inside the run function instead of opening the session in __init__

Answered By: DeadSec