简体   繁体   English

Flask:关闭数据库连接的方式,是否能够处理高流量?

[英]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. 我需要创建20个API才能使用我的应用。 These APIs, will fetch data from my MySQL database. 这些API将从我的MySQL数据库中获取数据。 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. 由于我预计高峰时段的流量会很高(高峰时段只有2小时),每秒可能会收到30到50个请求。

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? 由于一次只允许3个连接,因此使用我关闭游标的方法,数据库将能够处理高流量,并且用户能够毫无问题地访问20个API? The data I fetch is small, as my whole database in text is just merely 2MB. 我获取的数据很小,因为我的整个文本数据库只有2MB。

If this method can handle high traffic, I will not want to switch to sqlalchemy. 如果此方法可以处理高流量,我将不希望切换到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 ). 您可以使用http基准测试工具轻松地测试wsgi应用程序( 此处列出 ab,wrk等更多工具)。

Measure the time taken of your python functions and/or the mysql queries (very simple, timeit might be better): 测量您的python函数和/或mysql查询所花费的时间(非常简单,时间可能会更好):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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