简体   繁体   中英

python flask mysql using only one connection

When I get over 100 concurrent requests, mysql.connect() produces a "too many connections" error. I'm using a managed database which doesn't give me a root user to increase the connection limit. Below is the temporary fix that I need to replace.

import flaskext.mysql

@app.route("/filter")
def filter_ep():

    # FIXME: hot fix for "too many connections" error
    conn = None
    errs = 0
    while not conn and errs < 100:
        try:
            conn = mysql.connect()
        except Exception as e:
            errs += 1
            time.sleep(0.001)

    cur = con.cursor()
    # pull `results` from database
    cur.close()
    conn.close()

    return results

When I tried doing the same code using a single global connection I got packet out of order errors suggesting that cursors are reading each others responses.

I think the correct solution is to make some sort of task queue for queries but not sure how to implement.

This is my current solution, It's still bad but at least it doesn't consume all available connections causing other things to break.

# FIXME: this is still bad
conn = None
errs = 0
# for 1s try to connect until there's an open conn in queue
while not conn and errs < 100:
    try: 
        conn = mysql.connector.connect(pool_name="ropool", pool_size=4, **db.mysql_connection_args)
    except Exception as e:
        errs += 1
        time.sleep(0.01)
if not conn:
    return json.dumps({"errorMessage" : "failed to connect to database"}), 500

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