Reputation: 924
I need to create 20 APIs for the use of my app. These APIs, will fetch data from my MySQL database. Since I expected the traffic will be fairly high at peak hours (peak hours only 2 hours), and may get 30 to 50 request per second.
What I am concern is the connections. Since only 3 connections is allows at one time, with the method I close cursors and database will able to handle high traffic and user able to access 20 APIs with no issues? The data I fetch is small, as my whole database in text is just merely 2MB.
If this method can handle high traffic, I will not want to switch to sqlalchemy.
# A very simple Flask Hello World app for you to get started with...
from flask import Flask,jsonify,abort,make_response,request,render_template
import MySQLdb
import MySQLdb.cursors
app = Flask(__name__)
@app.route('/MUSIC', methods=['GET'])
def Music():
db = MySQLdb.connect(host='doreme.mysql.pythonanywhere-services.com',user='doreme',passwd='pw',db='doreme$default',cursorclass=MySQLdb.cursors.DictCursor)
curs = db.cursor()
try:
curs.execute("SELECT * FROM MUSIC")
a = curs.fetchall()
except Exception:
return 'Error: unable to fetch items'
finally:
curs.close()
db.close()
return jsonify({'Music': a})
@app.route('/MUSICKorea', methods=['GET'])
def MusicKorea():
db = MySQLdb.connect(host='doreme.mysql.pythonanywhere-services.com',user='doreme',passwd='pw',db='doreme$default',cursorclass=MySQLdb.cursors.DictCursor)
curs = db.cursor()
try:
curs.execute("SELECT * FROM MusicKorea")
b = curs.fetchall()
except Exception:
return 'Error: unable to fetch items'
finally:
curs.close()
db.close()
#return "hihi"
return jsonify({'Song': b})
@app.route('/MUSICKorea/<Item>', methods=['GET'])
def Musicitem(Korea):
db = MySQLdb.connect(host='doreme.mysql.pythonanywhere-services.com',user='doreme',passwd='pw',db='doreme$default',cursorclass=MySQLdb.cursors.DictCursor)
try:
curs.execute("SELECT * FROM MUSIC WHERE Song LIKE %s",(Korea,))
c = curs.fetchall()
except Exception:
return 'Error: unable to fetch items'
finally:
curs.close()
db.close()
return jsonify({'Korea': c})
Upvotes: 0
Views: 1664
Reputation: 18106
You can easily test your wsgi application using a http benchmark tool (ab, wrk, ... more tools are listed here).
Measure the time taken of your python functions and/or the mysql queries (very simple, timeit might be better):
import time
...
@app.route('/MUSICKorea/<Item>', methods=['GET'])
def Musicitem(Korea):
t = time.time()
db = MySQLdb.connect(host='doreme.mysql.pythonanywhere-services.com',user='doreme',passwd='pw',db='doreme$default',cursorclass=MySQLdb.cursors.DictCursor)
print('connecting took %.6f seconds' % (time.time()-t))
try:
curs.execute("SELECT * FROM MUSIC WHERE Song LIKE %s",(Korea,))
c = curs.fetchall()
except Exception:
return 'Error: unable to fetch items'
finally:
curs.close()
db.close()
print('Musicitem took %.6f seconds' % (time.time()-t))
return jsonify({'Korea': c})
Upvotes: 1