I'm creating a REST API. I'm using Python, Flask and MySQL to fetch data from the database and present them in the JSON format. But i've got an issue.
The REST API is served with NGINX and uWSGI and all works well. The index page stays available and doesn't time out but pages that make a connection to the database do. It takes about 15 minutes for pages to become unresponsive. This issue does not occur in a RDBMS like phpmyadmin or navicat.
What could it be and how can i fix it? The REST API code below.
# using python version 2.7.10
from flask import Flask, jsonify, request, session
import mysql.connector.pooling
#Make a connection with the DB
dbconfig = {
"host" : "12.34.5.78",
"database": "db",
"user": "user",
"password": "pass"
}
conn = mysql.connector.connect(pool_name = "mypool",
pool_size = 6,
**dbconfig)
#Define the root
app = Flask(__name__)
#Landings page
@app.route('/')
def index():
return "Hello World."
# return all resources by name
@app.route('/resources', methods=['GET'])
def allResourceNames():
conn1 = mysql.connector.connect(pool_name="mypool")
reader = conn1.cursor()
query = ("SELECT name FROM resources")
reader.execute(query)
resources = []
for name in reader:
resources.append({'name' : name[0]})
reader.close()
conn1.close()
return jsonify({"resources" : resources})
if __name__ == "__main__":
app.run(debug=True)