Flask/Apache中的PyMySQL有时返回空结果

loc*_*hok 7 python mysql apache flask pymysql

我有一个Flask应用程序,在Apache中运行,依赖于PyMySQL.该应用程序提供了一系列REST命令.它在Python 3下运行.

在不提供整个来源的情况下,该计划的结构如下:

#!flask/bin/python
import json
import pymysql
from flask import *

# Used to hopefully share the connection if the process isn't restarted
mysql_connection = None   

# Gets the mysql_connection, or opens it
GetStoreCnx():
    global mysql_connection
    if (mysql_connection != None):
        store_connection_string = ""
        # Get the connection string from the config file
        with open('config/storedb.json', 'r') as f:
            store_connection_string = json.load(f)
        mysql_connection = pymysql.connect(**store_connection_string)
    return mysql_connection;


class Server(Flask):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

# Return results via REST
@app.route('/results1', methods=['GET'])
def get_result1():
    cnx = GetStoreCnx();
    cursor = cnx.cursor();
    query = """
        SELECT 
            result_name,
            successful
        FROM
            results
        """
    cursor.execute(query)
    cnx.commit()
    result = cursor.fetchall()
    return json.dumps(result)

# Run server, if needed
if __name__ == '__main__':
    app.run(host='0.0.0.0', debug=True)
Run Code Online (Sandbox Code Playgroud)

还有一些REST调用 - 但它们基本上都做同样的事情(即 - 获取连接,创建游标,运行基本选择查询 - 有时会有超过2个字段,执行查询,获取结果和将其作为JSON对象返回).那里的提交应该是不必要的,但它似乎是PyMySQL的一个运行问题导致获取旧数据.

问题是这些REST调用有时会返回空的JSON集(即.[]).进一步的调查表明,执行调用有时会返回一个完全空的结果,但不会抛出异常.这种情况经常发生 - 但并非始终如此.有些调用会成功返回值.当我尝试保持呼叫进行直到它返回结果(如:

while(cursor.execute(query) < 1):
    pass
Run Code Online (Sandbox Code Playgroud)

)进程进入无限循环,最终(快速)阻止apache为更多请求提供服务.

服务器(目前)仅每秒服务大约5个呼叫.如果我使用开发服务器,则不会出现此问题.

有什么办法可以防止这个错误吗?这是PyMySQL的错吗?我正在做的事情阻止与MySQL的正确连接?

mat*_*ata 1

您正在创建一个供您的应用程序使用的全局 mysql 连接,悬停 pymysql 声明 athreadsafety为 1,根据dbapi2 规范,这意味着:

1   Threads may share the module, but not connections. 
Run Code Online (Sandbox Code Playgroud)

由于 Flask 中的并发请求将由不同的线程提供服务,因此您不应该共享连接。使用开发服务器时不会遇到任何问题的原因是它运行单线程。

为了避免这种情况,您可以:

  • 为每个线程创建一个新连接,将其存储为线程本地以供进一步使用
  • 为每个请求创建一个新连接,将其存储flask.g以供进一步使用

为此,您的GetStoreCnx函数可以这样更改:

import threading
thread_local = threading.local()

def GetStoreCnx():        
    if not hasattr(thread_local, 'mysql_connection'):
        store_connection_string = ""
        # Get the connection string from the config file
        with open('config/storedb.json', 'r') as f:
            store_connection_string = json.load(f)
        mysql_connection = pymysql.connect(**store_connection_string)
        thread_local.mysql_connection = mysql_connection
    return thread_local.mysql_connection;
Run Code Online (Sandbox Code Playgroud)

SQLAlchemy 对其进行了类似的操作scoped_session()。这也应该适用flask.g于每个请求而不是thread_local一个连接。