i am now creating a Flask App as a restful API where it receive data from another server, and the flask app will do some calculations using an AI model i created previously. I realized that whenever my flask was trying to retrieve previous data from the database to predict, the Waitress then tends to queue up the task.
what i did here was connecting to the MYSQL server database using pyodbc to retrieve previous data from another server DB. the following is as such:
run.py
from App import app
from waitress import serve
mode = 'prod'
if __name__ == "__main__":
if mode == 'prod':
serve(app, host='127.0.0.1',port=50100, threads = 1)
else:
app.run(host='127.0.0.1', port=50100)
Views.py
@app.route("/api/prediction", methods=["GET","POST"])
def prediction():
conn = pyodbc.connect("Driver= {ODBC Driver 17 for SQL Server};"
"SERVER= myserver;"
"database = mydatabase;"
"UID=sa;"
"password: providedpassword;")
mydata = pd.read_sql("select * from mydatabase.Table", conn)
*some data cleaning part*
conn.close()
*some other task to be done*
and whenever flask received a new data, the pyodbc was connected just as fine but it always stucked here, causing all the other task to queue and delayed for a long time. the following warning is such when pyodbc is retrieving data:
warning: waitress task queue depth is 1
warning: waitress task queue depth is 2
warning: waitress task queue depth is 3
warning: waitress task queue depth is 4
and after some time, the task will just continue on and not crash but it does delay the real-time data then. if i increase the number of thread to 4 by default, deadlock occured. do i have to do some multithreadings/multiprocessing?