I'm building a webapp, and one of my .py files keep a lot of SQL functions. These functions connect to the database, execute the query, fetch the result and close the connection.
Example:
def get_user_data():
conn = MySQLdb.connect(host, port, user, passwd, db)
cursor = conn.cursor()
query = "CALL `RANDOM_PROCEDURE`(%s)"
data = None
try:
cursor.execute(query,[random_param])
data = cursor.fetchone()
except Exception as error:
print(error)
return error
finally:
cursor.close()
conn.close()
return data
One of the pages run 3 of those functions before rendering the page:
@app.route('/app', methods=['POST', 'GET'])
def app_home():
if request.method == 'GET':
q1 = get_user_data()
q2 = get_weather_data()
q3 = get_product_data()
return render_template('app/index.html', param1=q1, param2=q2, param3=q3)
This page takes too long to load, since the queries are executed one after the other. I have tried to run them at the same time, in separate threads (using Process), but Flask tells me that I'm out of context.
Any suggestions?