Code is unreacheable due to scheduler while loop python flask

Solution 1:

I don't think you can run a blocking loop in the same scope you use your __main__ function.

I would say that you either create a thread to run the scheduler with the blocker loop in the background or alternatively you could use APScheduler.

from apscheduler.schedulers.background import BackgroundScheduler
    

def index():
    connection = db.engine.connect(close_with_result=True)
    print('first')

    sql = text("""delete from user""")
    print('done')
    connection.execute(sql)

    connection = db.engine.connect(close_with_result=True)

    sql = text("""select * from user where name='a' or name='b'""")
    connection.execute(sql)
    connection.close()

scheduler = BackgroundScheduler()
scheduler.add_job(func=index, trigger="interval", seconds=60)
scheduler.start()

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