简体   繁体   中英

Flask: the way closing database connections, will it able to handle high traffic?

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})

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})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM