I have a Flask application that runs on Apache that relies on PyMySQL. The application provides a number of REST commands. It works under Python 3.
Without providing an entire source, the program is structured as:
There are several more REST calls, but they all essentially do the same thing (i.e. get a connection, create a cursor, launch a basic select query, which sometimes has more than two fields, execute the query, fetch the result and return it as a JSON object ) The commits there should not be unnecessary, but it seems to be a problem with PyMySQL, which leads to getting old data.
The problem is that these REST calls sometimes return an empty JSON set (i.e. [] ). Further research showed that calling execute sometimes returns a completely empty result, but does not throw an exception. This happens regularly, but not all the time. Some calls successfully return values. When I try to save the call until it returns the result (like:
while(cursor.execute(query) < 1): pass
), the process goes into an endless cycle, ultimately (quickly), preventing Apache service from any other requests.
The server (currently) only serves 5 calls per second. The problem does not occur if I use the development server.
Can this error be prevented? Is this a bug in PyMySQL? Am I doing something to prevent proper MySQL connections?
python flask mysql apache pymysql
lochok
source share