flak long poll mysql database

I'm trying to long poll my sql database, and when a change occurs do some other stuff. in the request lastRequestTime is a number which is compared to the number in the field last by changing the number on either database or request it works, but I'm expecting inside the while True block to keep checking the dB. It seems it reads it once only.

(eventual timeout I can handle)

my code is

from flask import Flask
from flask import request
from flask import jsonify, make_response
from flask_mysqldb import MySQL
import time

app = Flask(__name__)

app.config['MYSQL_HOST'] = ''
app.config['MYSQL_USER'] = 'jamesdavies000'
app.config['MYSQL_PASSWORD'] = 'myPassword!!'
app.config['MYSQL_DB'] = 'jamesdavies000$default'
app.config['MYSQL_CURSORCLASS'] = 'DictCursor'
app.config['updated'] = False

mysql = MySQL(app)

@app.route("/poll", methods =['GET', 'POST'])
def poll():
    # request this and wait for response indicating change

    clientUpdateTime = request.args.get('lastRequestTime')
    cursor = mysql.connection.cursor()
    mySQLCommand = 'select Last from homeAutoPollCheck'

    while True:
        # poll sql for changes
        retVal = cursor.fetchall()
        valueDb = str(retVal[0]['Last'])
        if valueDb > clientUpdateTime:
            return valueDb, 201

The code you have there is not quite right, but unfortunately even with the appropriate fixes, that won't work on PythonAnywhere. The problem isn't so much the timeout, as how requests are processed. Your site has a certain number of processes handling all of the requests it receives. A free account would have one process, a Hacker account two, and so on.

While a worker process is handling one request, then it can't handle others. So let's say you had one person access the /poll view -- that would tie up one worker until it was timed out. If you have a Hacker account, you would now only have one worker available for all other requests to your site. Now let's say that another person accessed that view. Now both workers would be busy, so your site would not respond to any other requests.

I see. Do you know of a way to monitor database for changes from a client

I'm working on an android app with kivy to monitor the dB for changes and act accordingly.

The app would also make changes to the dB which I can do.

I can periodically poll via flask from the app, would this work every 10 seconds or so?


Yes, repeated polling with short request times will work fine.