I am trying to do a long poll using jQuery and Python under the framework jar.
After doing a long survey in PHP, I tried to do it the same way:
A script / function that has a while (true) loop, periodically checking for changes, for example, every 0.5 seconds in the database, and returns some data when changed.
So, in my ini .py, I created app.route for / poll to invoke jQuery. JQuery gives it some information about the current state of the client, and the poll () function compares this to what is currently in the database. The cycle is completed and returns information when a change is observed.
Here's the python code:
@app.route('/poll') def poll(): client_state = request.args.get("state")
The problem is that when the above code starts polling, the server becomes overloaded and other Ajax calls, and other requests, such as loading the “uploaded” image into html using jQuery, do not respond to requests and timeout.
For the full purpose, I have included jQuery here:
function poll() { queryString = "state="+JSON.stringify(currentState); $.ajax({ url:"/poll", data: queryString, timeout: 60000, success: function(data) { console.log(data); if(currentState == null) { currentState = JSON.parse(data); } else { console.log("A change has occurred"); } poll(); }, error: function(jqXHR, textStatus, errorThrown) { console.log(jqXHR.status + "," + textStatus + ", " + errorThrown); poll(); } }); }
Is it needed multithreaded or something like that? Or does anyone know why I experience this behavior?
Thanks in advance!:)